使用maxtasksperchild的python多处理

2024-04-26 17:44:59 发布

您现在位置:Python中文网/ 问答频道 /正文

编辑:我确认这是Python中的bug。它是bug http://bugs.python.org/issue10332(我提交了一个新的bug,维护人员给我指出了10332)。我将多处理目录从Python源repo复制到我的项目目录中,现在测试用例正常工作了。在

除非删除maxtasksperchild参数,否则这个看似简单的程序对我不起作用。我做错什么了?在

from multiprocessing import Pool
import os
import sys

def f(x):
  print "pid: ", os.getpid(), " got: ", x
  sys.stdout.flush()
  return [x, x+1]

def cb(r):
  print "got result: ", r

if __name__ == '__main__':
  pool = Pool(processes=1, maxtasksperchild=9)
  keys = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  result = pool.map_async(f, keys, chunksize=1, callback=cb)
  pool.close()
  pool.join()

当我运行它时,我得到:

^{pr2}$

它挂着。也就是说,处理第10个元素的新worker没有生成。在

在另一个终端,我看到:

$ ps -C python
  PID TTY          TIME CMD
 6408 pts/11   00:00:00 python
 6409 pts/11   00:00:00 python <defunct>

这是在运行python2.7.2+(从Ubuntu包安装)的ubuntu11.10上完成的。在


Tags: import目录osdefsysresultkeysbug
2条回答

maxtasksperchild表示一个处理执行的最大任务数

这个问题已经在python3中修复了。在

pid:  18316  got:  1
pid:  18316  got:  2
pid:  18316  got:  3
pid:  18316  got:  4
pid:  18316  got:  5
pid:  18316  got:  6
pid:  18316  got:  7
pid:  18316  got:  8
pid:  18316  got:  9
pid:  18317  got:  10
got result:  [[1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 7], [7, 8], [8, 9], [9, 10], [10, 11]]

相关问题 更多 >