views:

36

answers:

1

Out of curiosity and trying to understand the subprocess module: Is it possible to do something like:

import subprocess

def myfun(arg):
    # do stuff

arg = something;
p = subprocess.Popen(["myfun","arg"])

without putting "myfun" in a file of its own? It seems like this would in general be a scary thing to do if you are not careful about cleaning up child processes.

+2  A: 

You can pass Popen a preexec_fn, which is a callable object executed in the child process before the command is exec'd. A more standard approach is to use the multiprocessing module, which requires a Python function instead of an external command.

jleedev