""""
import os
import time
#fork只用于linux中
pid = os.fork()
print("hello")
if pid == 0:
print("子进程{},父进程是{}".format(os.getpid(),os.getppid()))
else:
print("父进程是{}".format(os.getppid()))
#加这个的目的是当父进程关闭的时候关闭子进程
time.sleep(2)
"""
from concurrent.futures import ProcessPoolExecutor
import multiprocessing
#多进程编程
import time
def get_html(n):
time.sleep(n)
print("sub_process")
return n
if __name__ == "__main__":
#process = multiprocessing.Process(target=get_html,args=(2,))
#process.start()
#process.join()
#print("main process")
#进程池
pool = multiprocessing.Pool(multiprocessing.cpu_count())
#result = pool.apply_async(get_html,args=(3,))
#等待所有任务完成加入
#pool.close()
#pool.join()
#print(result.get())
#imap
#for result in pool.imap(get_html,[1,5,3]):
# print("{} sleep success".format(result))
for result in pool.imap_unordered(get_html,[1,5,3]):
print("{} sleep success".format(result))