多处理:长期与短期工作者

时间:2017-07-28 18:36:40

标签: python multiprocessing python-multiprocessing

我正在尝试使用分布式队列进行多处理。是否有理由选择run而不是run2?我感觉run2稍微高一些,因为它没有将控制权返回给主进程的开销。

from multiprocessing import Pool
from foo import my_distributed_queue as queue

def process_msg(msg):
  foo(msg)

def run():
  pool = Pool(4)
  for msg in queue:
    pool.apply_async(process_msg, (msg, ))

def process_msg2():
    for msg in queue:
      foo(msg)

def run2():
  pool = Pool(4)
  for i in range(4):
      pool.apply_async(process_msg2, ())

if __name__ == '__main__'
  run()
  #run2()

0 个答案:

没有答案