我想知道是否可以使用子进程模块生成多个进程来运行在同一个脚本中定义的函数或方法(无需导入它)。 因此主脚本不等待执行完成。像这样(代码错了,但它说明了这个概念):
def printMe(arg):
print arg
myList=['One','Two','Three','Four','Five']
for word in myList:
printMe(word)
proc = subprocess.Popen(printMe(word), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
感谢您的评论!显然,当需要生成内部方法或函数时,需要使用多处理模块。似乎多处理模块方法pool.map()在用于将参数变量发送到由其调用的函数时,与“标准”函数的行为完全不同。
import os, sys
from multiprocessing import Pool
def printMe(arg):
arg+="_Completed"
return arg
myList=['One','Two','Three']
pool = Pool(processes=10)
results = pool.map(printMe, myList)
print results, type(results), len(results)
# Results to ['One_Completed', 'Two_Completed', 'Three_Completed'] <type 'list'> 3
SingleWord="Once_Upon_A_Time"
pool = Pool(processes=10)
results = pool.map(printMe, SingleWord)
# Results to: ['O_Completed', 'n_Completed', 'c_Completed', 'e_Completed', `'__Completed', 'U_Completed', 'p_Completed', 'o_Completed', 'n_Completed', '__Completed', 'A_Completed', '__Completed', 'T_Completed', 'i_Completed', 'm_Completed', 'e_Completed'] <type 'list'> 16`
答案 0 :(得分:3)
您可以使用多处理,而不必使用Pool。
import multiprocessing
def worker():
"""worker function"""
print 'Worker'
return
if __name__ == '__main__':
jobs = []
for i in range(5):
p = multiprocessing.Process(target=worker)
jobs.append(p)
p.start()
答案 1 :(得分:2)
这就是multiprocessing
成为标准库的原因。
from multiprocessing import Pool
def run(*args):
# this is the function to be run
return sum(*args)
if __name__ == "__main__":
pool = Pool(processes=10) # 10 processes
results = pool.map(run, [(1, 1, 1), (2, 2, 2), (3, 3, 3)])
print(results)
@Spuntnix至于您的更新。 pool.map
实际上期望第二个参数是可迭代的。因此,如果你给它一个字符串,它将迭代字符串并将每个字符作为参数发送。
我个人认为str不可迭代。另见:https://mail.python.org/pipermail/python-3000/2006-April/000759.html