简体   繁体   English

Python中的multiprocessing.Pool问题

[英]Problem with multiprocessing.Pool in Python

I am having a bit of trouble with Parallel processing in Python. 我在使用Python进行并行处理时遇到了麻烦。 I am completely new to the concept of Parallel computing. 我对并行计算的概念完全陌生。 I use multiprocessing that comes with standard Python. 我使用标准Python随附的多重处理。

I have 12 threads in my computer. 我的电脑中有12个线程。 I ask for 12 workers, but I am not always able to get all workers I ask for. 我要求12名工人,但我并不能总是得到我要求的所有工人。 My problem arises when I don't get access to as many workers I need to process nTasks number of tasks in my code below (currently set to four). 当我无法访问需要处理以下代码中的nTasks个任务(现在设置为四个)的任务时,就会出现我的问题。 What happens then is just that the code gets stuck and never gets to what is under the comment "# Get Results". 然后发生的只是代码被卡住,再也无法到达注释“#Get Results”下的内容。 It seems random how many workers I get (I always ask for 12), but the problem arises when I get three workers or less in the following code: 看来我得到多少个工人是随机的(我总是要求12个),但是当我在以下代码中得到三个或更少工人时,就会出现问题:

import multiprocessing as mp
import scipy as sp
import scipy.stats as spstat
import pylab

def testfunc(x0, N):
   print 'working with x0 = %s' % x0
   x = [x0]
   for i in xrange(1,N):
       x.append(spstat.norm.rvs(size = 1)) # stupid appending to make it slower
       if i % 10000 == 0:
          print 'x0 = %s, i = %s' % (x0, i)
   return sp.array(x)

def testfuncParallel(fargs):
    return testfunc(*fargs)

pool = mp.Pool(12) # I have 12 threads

nTasks = 4
N = 100000

tasks = [(x, n) for x, n in enumerate(nTasks*[N])] # nTasks different tasks

result = pool.map(testfuncParallel, tasks)
pool.close()
pool.join()

# Get results:
sim = sp.zeros((N, nTasks)) 

for nn, res in enumerate(result):    
    sim[:, nn] = res

pylab.figure()
for i in xrange(nTasks):
    pylab.subplot(nTasks,1, i + 1)
    pylab.plot(sim[:, i])

pylab.show()

I have tried to use pool.map_async instead of pool.map but I cannot get around the problem. 我尝试使用pool.map_async而不是pool.map,但无法解决问题。

Thanks in advance, 提前致谢,

Sincerely, 真诚的

Matias 马蒂亚斯

It turned out that this was only a problem when running the Debug mode in the Wing Editor, see Stephan Deibels comment in my other question multiprocessing.Pool seems to work in Windows but not in ubuntu? 原来这只是在Wing Editor中运行Debug模式时的一个问题,请参阅我在其他问题multiprocessing中的 Stephan Deibels注释。Pool 似乎可以在Windows中工作,但不能在ubuntu中工作? .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM