我希望我的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))
感谢您的建议。我有点难以正确地制定问题,从而进行适当的网络搜索。
答案 0 :(得分:6)
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
命令非常相似,不同之处在于它将任务分配给工作池。