1.linux多進程html
import os import time pid = os.fork() if pid == 0: print('子進程:{}父進程{}'.format(os.getpgid(),os.getppid())) else: print('我是父進程{}'.format(pid)) time.sleep(2)
2.跨平臺多進程multiprocessinglinux
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.使用進程池app
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.進程間的通訊async
使用queueide
from multiprocessing import Manager,Queue,Process import time def shengchan(que): n = 0 while True: time.sleep(1) n+=1 print('生產者{}'.format(n)) que.put(n) def xiaofeizhe(que): while True: time.sleep(1) print('消費者{}'.format(que.get())) if __name__=='__main__': queue = Queue(10) pro = Process(target=shengchan, args=(queue,)) pro1 = Process(target=xiaofeizhe , args=(queue,)) pro.start() pro1.start() pro.join() pro1.join()
使用Managerspa
使用進程池的話 沒法用queue進行通訊 就要使用到Manager.queue進行通訊code
import time from concurrent.futures import ProcessPoolExecutor from multiprocessing import Manager,Pool,cpu_count def shengchan(que): n = 0 while True: n+=1 print('生產者{}'.format(n)) que.put(n) time.sleep(1) def xiaofeizhe(que): while True: print('消費者{}'.format(que.get())) time.sleep(1) if __name__=='__main__': que = Manager().Queue(10) pool = Pool(cpu_count()) result = pool.apply_async(shengchan, args=(que,)) result1 = pool.apply_async(xiaofeizhe, args=(que,)) #等待全部任務完成 pool.close() pool.join()
使用pipeorm
import time from concurrent.futures import ProcessPoolExecutor from multiprocessing import Manager,Pool,cpu_count,Pipe def shengchan(pipe): n = 0 while True: n+=1 print('生產者{}'.format(n)) pipe.send(n) time.sleep(1) def xiaofeizhe(pipe): while True: print('消費者{}'.format(pipe.recv())) time.sleep(1) if __name__=='__main__': recevie_pipe, send_pipe = Pipe() pool = Pool(cpu_count()) result = pool.apply_async(shengchan, args=(send_pipe,)) result1 = pool.apply_async(xiaofeizhe, args=(recevie_pipe,)) #等待全部任務完成 pool.close() pool.join()