1

我希望我的 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))

谢谢你的建议。对我来说,正确地表述问题并因此进行适当的网络搜索有点困难。

4

1 回答 1

7

使用多处理模块

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])

The pool, be default, will create N worker processes, where N is the number of cpus (or cores) the machine possesses. pool.map behaves much like the Python builtin map command, except that it farms out tasks to the pool of workers.

于 2012-10-10T03:04:00.877 回答