简体   繁体   中英

python subprocess calling “internal” process

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM