Skip to content

Instantly share code, notes, and snippets.

@criloz
Created March 10, 2015 09:23
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/9b7d598cc4134f3ee7ff to your computer and use it in GitHub Desktop.
Save criloz/9b7d598cc4134f3ee7ff to your computer and use it in GitHub Desktop.
is_parent_running with os.getppid()
from multiprocessing import Process
import time
import os
def is_parent_running():
try:
os.kill(os.getppid(), 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()
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
6013
True
6013
True
6013
True
6013
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
6013
True
6013
True
6013
True
6013
True
6013
True
6013
True
6013
True
6013
True
6013
True
6013
True

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