Python 多进程池
def get_html(n):
time.sleep(n)
print("sub_progress success")
return n
# 多进程池
pool = multiprocessing.Pool(multiprocessing.cpu_count()) result = pool.apply_async(get_html, args=(3,)) # 类似线程池中的sumbmit #等待所有任务完成 pool.close() # 在join前,必须先关闭,停止接收 pool.join() print(result.get())
def get_html(n):
time.sleep(n)
print("sub_progress success")
return n
pool = multiprocessing.Pool(multiprocessing.cpu_count())
for result in pool.imap(get_html, [1,5,3]): # 会按照顺序输出 print("{} sleep success".format(result))
def get_html(n): time.sleep(n) print("sub_progress success") return n pool = multiprocessing.Pool(multiprocessing.cpu_count()) for result in pool.imap_unordered(get_html, [1,5,3]): # 不会按照顺序输出 print("{} sleep success".format(result))