Ipython and fork ()

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.

The twist is that I need to be able to run the script from both the Unix shell using python and ipython using %run .

How will child processes end so as not to enter ipython command line? In my experience, sys.exit() will not do.

+7
source share
2 answers

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:

Note The standard way to exit sys.exit(n) . _exit() should normally be used only in the child process after fork ().

+8
source

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?

0
source

All Articles