如何在Python中将函数作为子进程运行?

时间:2012-10-10 02:59:37

标签: python subprocess

我希望我的Python脚本能够将其中一个函数作为子进程运行。我该怎么做?

这是我的意图的模拟脚本:

#!/urs/bin/env python

def print_mynumber(foo):
    """This function is obviously more complicated in my script.
    It should be run as a subprocess."""
    print(foo)

for foo in [1,2,3]:
    print_mynumber(foo) # Each call of this function should span a new process.
    # subprocess(print_mynumber(foo))

感谢您的建议。我有点难以正确地制定问题,从而进行适当的网络搜索。

1 个答案:

答案 0 :(得分:6)

使用multiprocessing module

import multiprocessing as mp

def print_mynumber(foo):
    """This function is obviously more complicated in my script.
    It should be run as a subprocess."""
    print(foo)

if __name__ == '__main__':
    for foo in [1,2,3]:
        proc = mp.Process(target = print_mynumber, args = (foo, ))
        proc.start()

您可能不希望为每次调用print_mynumber创建一个进程,特别是如果列表foo迭代很长时间。在这种情况下,更好的方法是使用多处理池:

import multiprocessing as mp

def print_mynumber(foo):
    """This function is obviously more complicated in my script.
    It should be run as a subprocess."""
    print(foo)

if __name__ == '__main__':
    pool = mp.Pool()
    pool.map(print_mynumber, [1,2,3])

默认情况下,池将创建N个工作进程,其中N是计算机拥有的cpus(或核心)数。 pool.map的行为与Python内置map命令非常相似,不同之处在于它将任务分配给工作池。