Skip to content

Instantly share code, notes, and snippets.

@vitalizzare
Created June 1, 2021 16:54
Show Gist options
  • Select an option

  • Save vitalizzare/9083b2828203d03515a2fbfdb8e65309 to your computer and use it in GitHub Desktop.

Select an option

Save vitalizzare/9083b2828203d03515a2fbfdb8e65309 to your computer and use it in GitHub Desktop.
Finish processing the data neatly, save the state and exit by pressing Ctrl-C (Python CLI)
from signal import SIGINT, signal
from time import sleep
import pickle
def load(store):
try:
with open(store, 'br') as f:
start = pickle.load(f)
except Exception:
start = None
return start
def dump(data, store):
try:
with open(store, 'wb') as f:
pickle.dump(next(data), f)
except Exception:
with open(store, 'wb') as f:
pickle.dump(None, f)
def source(start=None):
'''Mock data source'''
if start is None:
start = 0
yield from range(start, 30)
def process(data):
'''Mock process'''
sleep(1); print(data)
def trap_sigint(sig, frame):
if input(' Break? [y]es, [N]o').startswith('y'):
global save_and_exit
save_and_exit = True
def main(store='saved_data'):
...
data = source(load(store))
global save_and_exit
save_and_exit = False
try:
_ = signal(SIGINT, trap_sigint)
print('Press Ctrl-C to stop.')
while not save_and_exit:
process(next(data))
except StopIteration:
dump(None, store)
else:
dump(data, store)
finally:
signal(SIGINT, _)
...
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment