python多处理函数返回的多个输出
我尝试使用多处理返回列表,但不是等待所有进程完成,而是从mp_factorizer中的一个return语句中获得多个返回,如下所示:python多处理函数返回的多个输出,python,windows-7,multiprocessing,Python,Windows 7,Multiprocessing,我尝试使用多处理返回列表,但不是等待所有进程完成,而是从mp_factorizer中的一个return语句中获得多个返回,如下所示: None None (returns list) 在这个例子中,我使用了两个线程。如果我使用了5个线程,那么在列表输出之前将有5个None返回。代码如下: def mp_factorizer(nums, nprocs, objecttouse): if __name__ == '__main__': out_q = multiproces
None
None
(returns list)
在这个例子中,我使用了两个线程。如果我使用了5个线程,那么在列表输出之前将有5个None返回。代码如下:
def mp_factorizer(nums, nprocs, objecttouse):
if __name__ == '__main__':
out_q = multiprocessing.Queue()
chunksize = int(math.ceil(len(nums) / float(nprocs)))
procs = []
for i in range(nprocs):
p = multiprocessing.Process(
target=worker,
args=(nums[chunksize * i:chunksize * (i + 1)],
out_q,
objecttouse))
procs.append(p)
p.start()
# Collect all results into a single result dict. We know how many dicts
# with results to expect.
resultlist = []
for i in range(nprocs):
temp=out_q.get()
index =0
for i in temp:
resultlist.append(temp[index][0][0:])
index +=1
# Wait for all worker processes to finish
for p in procs:
p.join()
resultlist2 = [x for x in resultlist if x != []]
return resultlist2
def worker(nums, out_q, objecttouse):
""" The worker function, invoked in a process. 'nums' is a
list of numbers to factor. The results are placed in
a dictionary that's pushed to a queue.
"""
outlist = []
for n in nums:
outputlist=objecttouse.getevents(n)
if outputlist:
outlist.append(outputlist)
out_q.put(outlist)
mp#u factorizer获取一个项目列表、线程列表和工作线程应该使用的对象,然后将项目列表拆分,以便所有线程都获得相同数量的列表,并启动工作线程。
然后,工作人员使用该对象从给定列表中计算一些内容,并将结果添加到队列中。
Mp_factorizer应该收集队列中的所有结果,将它们合并到一个大列表中并返回该列表。然而,我得到了多重回报
我做错了什么?或者这种预期行为是由于windows处理多处理的奇怪方式造成的?
(Python 2.7.3,Windows7 64位)
编辑:
问题是
的位置错误,如果
。我在处理另一个问题时发现了这个问题,请参阅以获得完整的解释。如果您的语句
位于错误的位置。将其放在print
语句周围,以防止子进程执行该行:
if __name__ == '__main__':
print mp_factorizer(list, 2, someobject)
现在,在
mp_factorizer
中有了if
,当在子流程中调用时,该函数将返回None
如果您的语句位于错误的位置。将其放在print
语句周围,以防止子进程执行该行:
if __name__ == '__main__':
print mp_factorizer(list, 2, someobject)
现在,在mp_factorizer
中有了if
,当在子流程中调用时,该函数将返回None
如果uuuu name uuu=='\uuuu main uuu'
位于错误的位置。一个快速解决方案是只保护对mp_factorizer的调用,如Janne Karila建议的:
if __name__ == '__main__':
print mp_factorizer(list, 2, someobject)
但是,在windows上,主文件将在执行时执行一次+每个工作线程执行一次,在本例中为2。因此,这将是主线程总共执行3次,不包括代码的受保护部分
一旦在同一主线程中进行其他计算,这可能会导致问题,至少会不必要地降低性能。即使只有辅助函数应该执行多次,但在windows中,如果
中的不受保护,则会执行所有不受保护的函数
因此,解决方案将是通过仅在之后执行所有代码来保护整个主进程
如果uuuu name uuuu=='\uuuuuu main\uuuuu'
但是,如果辅助函数位于同一文件中,则需要将其从该If语句中排除,因为否则无法多次调用它进行多处理
伪代码主线程:
# Import stuff
if __name__ == '__main__':
#execute whatever you want, it will only be executed
#as often as you intend it to
#execute the function that starts multiprocessing,
#in this case mp_factorizer()
#there is no worker function code here, it's in another file.
即使整个主进程都受到保护,但只要辅助函数位于另一个文件中,它仍然可以启动
伪代码主线程,带辅助函数:
# Import stuff
#If the worker code is in the main thread, exclude it from the if statement:
def worker():
#worker code
if __name__ == '__main__':
#execute whatever you want, it will only be executed
#as often as you intend it to
#execute the function that starts multiprocessing,
#in this case mp_factorizer()
#All code outside of the if statement will be executed multiple times
#depending on the # of assigned worker threads.
有关可运行代码的详细说明,请参见
如果
位于错误的位置。一个快速解决方案是只保护对mp_factorizer的调用,如Janne Karila建议的:
if __name__ == '__main__':
print mp_factorizer(list, 2, someobject)
但是,在windows上,主文件将在执行时执行一次+每个工作线程执行一次,在本例中为2。因此,这将是主线程总共执行3次,不包括代码的受保护部分
一旦在同一主线程中进行其他计算,这可能会导致问题,至少会不必要地降低性能。即使只有辅助函数应该执行多次,但在windows中,如果中的不受保护,则会执行所有不受保护的函数
因此,解决方案将是通过仅在之后执行所有代码来保护整个主进程
如果uuuu name uuuu=='\uuuuuu main\uuuuu'
但是,如果辅助函数位于同一文件中,则需要将其从该If语句中排除,因为否则无法多次调用它进行多处理
伪代码主线程:
# Import stuff
if __name__ == '__main__':
#execute whatever you want, it will only be executed
#as often as you intend it to
#execute the function that starts multiprocessing,
#in this case mp_factorizer()
#there is no worker function code here, it's in another file.
即使整个主进程都受到保护,但只要辅助函数位于另一个文件中,它仍然可以启动
伪代码主线程,带辅助函数:
# Import stuff
#If the worker code is in the main thread, exclude it from the if statement:
def worker():
#worker code
if __name__ == '__main__':
#execute whatever you want, it will only be executed
#as often as you intend it to
#execute the function that starts multiprocessing,
#in this case mp_factorizer()
#All code outside of the if statement will be executed multiple times
#depending on the # of assigned worker threads.
有关可运行代码的详细解释,请参见您所说的“我从一个返回语句中获得多个返回”是什么意思?另外,请发布一个可运行的示例。我将发布一个示例,但需要一段时间才能确保该示例重现问题。通过多次返回,我的意思是,如果我编写print
mp\u factorizer(list,2,someobject)
,我不会让print命令执行一次,而是一次加上设置线程数的次数,即使用两个线程:None(prints list)
(每一行中的一个)print应该只执行一次。但我会得到3份打印件。所以事实上,每次worker完成(?)时,以及在列表返回时,都会执行return语句。你说的“我从一个return语句中获得多个返回”是什么意思?另外,请发布一个可运行的示例。我将发布一个示例,但需要一段时间才能确保该示例重现问题。通过多次返回,我的意思是,如果我编写printmp\u factorizer(list,2,someobject)
,我不会让print命令执行一次,而是一次加上设置线程数的次数,即使用两个线程:None(prints list)
(每一行中的一个)print应该只执行一次。但我会得到3份打印件。因此,事实上,每次worker完成(?)时,以及在列表返回时,都会执行return语句。我昨天也发现了自己。但是,仅保护调用mp_fac的部分