import multiprocessing
import time
def func(msg):
time.sleep(1)
print multiprocessing.current_process().name + '-' + msg
def spider():
time.sleep(2)
print multiprocessing.current_process().name + '-' + msg
try:
pool1 = multiprocessing.Pool(processes=10)
for i in [page for page in range(100)]:
msg = "word %d" % (i)
print "msg",msg
pool1.apply_async(spider, (msg,))
pool1.close()
pool1.join()
except Exception as e :
print e
if __name__ == "__main__":
start_time = time.time()
pool = multiprocessing.Pool(processes=10)
results = []
for i in [page for page in range(10)]:
msg = "hello %d" % (i)
results.append(pool.apply_async(func, (msg,)))
pool.close()
pool.join()
print time.time()-start_time
子进程不能在通过multiprocessing创建后代进程,否则当父进程退出后,它终结其daemon子进程,那孙子进程就成了孤儿进程了。当尝试这么做时,会报错:AssertionError: daemonic processes are not allowed to have children