0

我正在尝试使用 python 的 pool 模块编写一个简单的程序。这是我的代码:

from multiprocessing import Pool
from multiprocessing.pool import ApplyResult
import time
import os

def unwrap_self_f(arg, **kwarg):
  print 'inside unwrap_self_f'
  return C().f(arg, **kwarg)

class C:
  def f(self, name):
    return 'Inside f..Process id :' + str(os.getpid())

  def run(self):
    print 'Reached inside run..'
    print 'main process id ..', os.getpid()
    pool = Pool(processes=4)
    names = ['frank', 'justin', 'osi', 'thomas']        
    print pool.map(unwrap_self_f, names, 1)               

if __name__ == '__main__':
   print 'Starting....'
   c = C()
   print 'Running....'
   c.run()

当我运行这个程序时,我期望看到 4 个不同的进程 id 被打印出来,每个创建的子进程一个。我选择的块大小为 1,因此列表中的每个项目都进入一个单独的进程。

但是,正在打印的进程 ID 是相同的。我得到以下输出:

Starting....
Running....
Reached inside run..
main process id .. 1284
['Inside f..Process id :6872', 'Inside f..Process id :6872', 'Inside f..Process id :6872', 'Inside f..Process  id :6872']

我是不是在这里搞错了。请任何人都可以对此有所了解。提前致谢。

4

0 回答 0