# -*- coding:utf-8 -*-
from multiprocessing import Pool
import os, time, random
def worker(msg):
t_start = time.time()
print("%s开始执行,进程号为%d" % (msg, os.getpid()))
time.sleep(random.random() * 2)
t_stop = time.time()
print(msg, "执行完毕,耗时%0.2f" % (t_stop - t_start))
po = Pool(3)
for i in range(0, 10):
po.apply_async(worker, (i,))
print("----start----")
po.close()
po.join()
print("-----end-----")
python多进程 没有必需__name__="__main__"。
但windows 平台有特殊要求Since Windows has no fork, the multiprocessing module starts a new Python process and imports the calling module. If Process() gets called upon import, then this sets off an infinite succession of new processes (or until your machine runs out of resources). This is the reason for hiding calls to Process() inside
if __name__ == “__main__”
since statements inside this if-statement will not get called upon import.
windows中没有fork()函数,如果不加if __name__ == "__main__"会造成引用循环