Skip to content

Instantly share code, notes, and snippets.

@criloz
Created March 10, 2015 09:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save criloz/8f3e3e5aef510250a7cc to your computer and use it in GitHub Desktop.
Save criloz/8f3e3e5aef510250a7cc to your computer and use it in GitHub Desktop.
is_parent_running with parent_pid
from multiprocessing import Process
import time
import os
def is_parent_running(pid):
try:
os.kill(pid, 0)
return True
except OSError:
return False
def child_waiting_for_parent(parent_pid):
print "waiting for parent to exit"
while True:
time.sleep(1)
print parent_pid
print is_parent_running(parent_pid)
if __name__ == "__main__":
p = Process(target=child_waiting_for_parent, args=(os.getpid(),))
p.start()
while True:
time.sleep(5)
# random error
kofsaasnf
@criloz
Copy link
Author

criloz commented Mar 10, 2015

/home/cristian/PycharmProjects/untitled4/bin/python2.7 /home/cristian/PycharmProjects/untitled4/test_wait.py
waiting for parent to exit
6355
True
6355
True
6355
True
6355
True
Traceback (most recent call last):
  File "/home/cristian/PycharmProjects/untitled4/test_wait.py", line 31, in <module>
    kofsaasnf
NameError: name 'kofsaasnf' is not defined
6355
True
6355
True
6355
True
6355
True
6355
True
6355
True
6355
True
6355
True
6355
True
6355
True
6355
True
6355
True
6355
True
6355
True
6355
True
6355
True
6355
True
6355
True
6355
True
6355
True
6355
True
6355
True
6355
True
6355
True```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment