1.linux多进程
import os import time pid = os.fork() if pid == 0: print(‘子进程:{}父进程{}‘.format(os.getpgid(),os.getppid())) else: print(‘我是父进程{}‘.format(pid)) time.sleep(2)
2.跨平台多进程multiprocessing
import time from concurrent.futures import ProcessPoolExecutor import multiprocessing def get_html(n): time.sleep(n) print(n) print(33) return n if __name__==‘__main__‘: pro = multiprocessing.Process(target=get_html, args=(2,)) pro.start() pro.join()
3.使用进程池
import time from concurrent.futures import ProcessPoolExecutor import multiprocessing def get_html(n): print(‘逆火‘) time.sleep(n) print(n) return n if __name__==‘__main__‘: print("合数{}".format(multiprocessing.cpu_count())) Pool = multiprocessing.Pool(multiprocessing.cpu_count()) # result = Pool.apply_async(get_html, args=(3,)) # #等待所有任务完成 # Pool.close() # Pool.join() # print(result.get()) for ret in Pool.imap(get_html, [1,5,3]): print(‘返回值{}‘.format(ret))
4.进程间的通信
原文:https://www.cnblogs.com/gaosie/p/10816690.html