I plan to use a Python script that will use os.fork() to create a bunch of child processes to do some calculations. The parental process will be blocked until the children are over.
os.fork()
The twist is that I need to be able to run the script from both the Unix shell using python and ipython using %run .
python
ipython
%run
How will child processes end so as not to enter ipython command line? In my experience, sys.exit() will not do.
sys.exit()
The following seems to work:
import os, sys child_pid = os.fork() if child_pid == 0: print 'in child' os._exit(os.EX_OK) print 'hm... wasn''t supposed to get here' else: print 'in parent'
The trick is to use os._exit() instead of sys.exit() . The documentation contains the following excerpt:
os._exit()
Note The standard way to exit sys.exit(n) . _exit() should normally be used only in the child process after fork ().
sys.exit(n)
_exit()
In Linux terminology, instead of the fork the iPython process, why don't you execute the python regular interpreter from the iPython shell and have one fork?
fork