第一种写法,多进程的函数是没有返回值的,需要放在Queue中来取值
import multiprocessing as mp
def job(char, num, q):
q.put([char] * num)
if __name__ == "__main__":
q = mp.Queue()
p1 = mp.Process(target=job, args=("a", 3, q))
p2 = mp.Process(target=job, args=("b", 4, q))
p1.start()
p2.start()
p1.join()
p2.join()
print(q.get())
print(q.get())
join()
用于等待子进程退出,输出为
['b', 'b', 'b', 'b']
['a', 'a', 'a']
第二种写法,线程池
import multiprocessing as mp
def job(char, num):
return [char] * num
if __name__ == "__main__":
with mp.Pool(processes=mp.cpu_count() - 1) as pool:
p1 = pool.apply_async(job, ('a', 3))
p2 = pool.apply_async(job, ('b', 4))
print(p1.get(timeout=1))
print(p2.get(timeout=1))
输出类似
['a', 'a', 'a']
['b', 'b', 'b', 'b']
其中这个pool.join()
要不要加好像不要紧,参考这个回答when-should-we-call-multiprocessing-pool-join
第三种写法:
import multiprocessing as mp
def job(char, num):
return [char] * num
if __name__ == "__main__":
args = [("a", 4), ("b", 3)]
with mp.Pool(processes=mp.cpu_count() - 1) as pool:
results = pool.starmap(job, args)
print(results)
返回值
[['a', 'a', 'a', 'a'], ['b', 'b', 'b']]
非常类似的写法:
import multiprocessing as mp
def job(char, num):
return [char] * num
if __name__ == "__main__":
args = [("a", 4), ("b", 3)]
with mp.Pool(processes=mp.cpu_count() - 1) as pool:
results = pool.starmap_async(job, args)
print(results.get(timeout=1))
参考文献
stackoverflow: python multiprocessing pool map for multiple arguments
https://docs.python.org/3/library/multiprocessing.html