fork - Forking and exiting from child in python -
fork - Forking and exiting from child in python -
i'm trying fork process, in kid , exit (see code below). exit first tried sys.exit turned out problem because intermediate function caught systemexit exception (as in code below) , kid didn't terminate. figured out should utilize os._exit instead. kid terminates, still see defunct processes lying around (when ps -ef). there way avoid these?
import os, sys def fctn(): if os.fork() != 0: homecoming 0 # sys.exit(0) os._exit(0) while true: str = raw_input() try: print(fctn()) except systemexit: print('caught systemexit.') edit: not python question more of unix question (so guess results may vary depending on system). ivan's reply suggests should like
def handlesigchld(sig, frame): os.wait() signal.signal(signal.sigchld, handlesigchld) while me simple
signal.signal(signal.sigchld, signal.sig_ign) also works.
and it's true should utilize library...
you should wait() kid remove zombie process entry table.
finally, offload tasks children, may improve off multiprocessing.
python fork system exit
Comments
Post a Comment