我正在尝试使用分布式队列进行多处理。是否有理由选择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()