在学习多进程之前先看下面一段代码
# import os # #fork只能用于linux/unix中 # pid = os.fork() # print("bobby") # if pid == 0: # print('子进程 {} ,父进程是: {}.' .format(os.getpid(), os.getppid())) # else: # print('我是父进程:{}.'.format(pid))
执行结果是:
子进程会将父进程所有数据原样拷贝一份到子进程当中(fork之后的代码),每一个进程有一套自己完全隔离的数据
from concurrent.futures import ProcessPoolExecutor
多进程和多线程使用方法差不多的,具体就不详解了。。。。。。。
ProcessPoolExecutor里面其实调用的就是multiprocessing(更接近底层),具体介绍下multiprocessing的用法
import multiprocessing #多进程编程 import time def get_html(n): time.sleep(n) print("sub_progress success") return n if __name__ == "__main__": # progress = multiprocessing.Process(target=get_html, args=(2,)) # print(progress.pid) # progress.start() # print(progress.pid) # progress.join() # print("main progress end") #使用线程池 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))
进程间的通讯:
#共享全局变量通信
#共享全局变量不能适用于多进程编程,可以适用于多线程
Queue:
from multiprocessing import Process, Queue
import time from multiprocessing import Process,Queue def producer(queue): queue.put('a') time.sleep(2) def consumer(queue): time.sleep(2) data=queue.get() print(data) if __name__ == '__main__' : queue = Queue(10) my_producer = Process(target=producer,args=(queue,)) my_consumer = Process(target=consumer,args=(queue,)) my_producer.start() my_consumer.start() my_producer.join() my_consumer.join()
#multiprocessing中的queue不能用于pool进程池
#pool中的进程间通信需要使用manager中的queue
import time from multiprocessing import Process,Queue,Pool,Manager def producer(queue): queue.put('a') time.sleep(2) def consumer(queue): time.sleep(2) data=queue.get() print(data) if __name__ == '__main__' : queue = Manager().Queue(10) pool = Pool(2) pool.apply_async(producer, args=(queue,)) pool.apply_async(consumer, args=(queue,)) pool.close() pool.join()
#通过pipe实现进程间通信
#pipe的性能高于queue
import time from multiprocessing import Process,Queue,Pool,Manager,Pipe def producer(pipe): pipe.send("body") def consumer(pipe): print(pipe.recv()) if __name__ == '__main__' : recevie_pipe,send_pipe = Pipe() #pipe只能适用于两个进程 my_producer= Process(target=producer, args=(send_pipe, )) my_consumer = Process(target=consumer, args=(recevie_pipe,)) my_producer.start() my_consumer.start() my_producer.join() my_consumer.join()
共享内存:
def add_data(p_dict, key, value): p_dict[key] = value if __name__ == "__main__": progress_dict = Manager().dict() from queue import PriorityQueue first_progress = Process(target=add_data, args=(progress_dict, "bobby1", 22)) second_progress = Process(target=add_data, args=(progress_dict, "bobby2", 23)) first_progress.start() second_progress.start() first_progress.join() second_progress.join() print(progress_dict)
本文来自博客园,作者:孙龙-程序员,转载请注明原文链接:https://www.cnblogs.com/sunlong88/articles/9480783.html