1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
|
import os, sys
def restart_process():
import sys
os.execv(sys.executable, [sys.executable] + sys.argv)
def restartable_point_fork(auto=None, extra_msg=None):
while True:
while True:
if extra_msg:
print extra_msg
print '---> Checkpoint: cont / restart-it-all / quit / pdb ?'
if auto:
print 'auto-%s' % (auto,)
line = auto
auto = None
else:
try:
line = raw_input().strip().lower()
except (KeyboardInterrupt, EOFError) as e:
print '(%s ignored)' % e.__class__.__name__
continue
if line in ('run', 'cont'):
break
if line == 'quit':
raise SystemExit
if line == 'pdb':
try:
import pdb; pdb.set_trace()
dummy_for_pdb = 1 # for pdb to land
except Exception as e:
print '(%s ignored)' % e.__class__.__name__
continue
if line == 'restart-it-all':
restart_process()
try:
pid = os.fork()
except AttributeError:
# windows case
return
if pid != 0:
# in parent
while True:
try:
pid, status = os.waitpid(pid, 0)
except KeyboardInterrupt:
continue
else:
break
print
print '_'*78
print 'Child %d exited' % pid,
if os.WIFEXITED(status):
print '(exit code %d)' % os.WEXITSTATUS(status)
elif os.WIFSIGNALED(status):
print '(caught signal %d)' % os.WTERMSIG(status)
else:
print 'abnormally (status 0x%x)' % status
continue
# in child
print '_'*78
break
# special version for win32 which does not have fork() at all,
# but epople can simulate it by hand using VMware
def restartable_point_nofork(auto=None):
# auto ignored, no way to automate VMware, yet
restartable_point_fork(None, '+++ this system does not support fork +++\n'
'if you have a virtual machine, you can save a snapshot now')
if sys.platform == 'win32':
restartable_point = restartable_point_nofork
else:
restartable_point = restartable_point_fork
if __name__ == '__main__':
print 'doing stuff...'
print 'finished'
restartable_point()
print 'doing more stuff'
print 'press Enter to quit...'
raw_input()
|