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()