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