获取worker返回的数据

时间:2013-04-19 07:33:09

标签: python multithreading multiprocessing

我如何获取工人返回的数据?

import multiprocessing

def worker():

      """worker function"""
      return 'DATA'

if __name__ == '__main__':

      jobs = []
      for i in range(5):
          p = multiprocessing.Process(target=worker)
          print p
          jobs.append(p)
          p.start()

输出:

DATA
DATA
DATA
DATA
DATA

1 个答案:

答案 0 :(得分:1)

您可以创建一个工作池并传入一些数据来处理:

import multiprocessing

def worker(item):
    return item ** 2

if __name__ == '__main__':
    pool = multiprocessing.Pool(5)

    for result in pool.imap_unordered(worker, range(30)):
        print result