进程间通信的4种方式

python提供了4种方式来满足进程间的数据通信

1. 使用multiprocessing.Queue可以在进程间通信,但不能在Pool池创建的进程间进行通信

2. 使用multiprocessing.Manager.Queue可以在Pool进程池创建的进程间进行通信

3. 通过Pipe进行线程间的通信, pipe进程间通信的性能高于Queue,但是它只能在两个进程间进行通信

4. 使用Manager类提供的数据结构可以进行进程间的通信

from multiprocessing import Process, Queue, Pool, Manager, Pipe
# 注意线程间的通信,使用的queue.Queue
# from  queue import Queue
import time


# 1. 使用multiprocessing.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)
#     p = Process(target=producer, args=(queue,))
#     c = Process(target=consumer, args=(queue,))
#     p.start()
#     c.start()
#     p.join()
#     c.join()


# 2. 使用共享全局变量,在多进程间通信(结论: 不行)
# def producer(a):
#     a += 1
#     time.sleep(2)
#
#
# def consumer(a):
#     time.sleep(2)
#     print(a)
#
# if __name__ == '__main__':
#     a = 1
#     p = Process(target=producer, args=(a,))
#     c = Process(target=consumer, args=(a,))
#     p.start()
#     c.start()
#     p.join()
#     c.join()


# 3. multiprocessing.Queue不能用于multiprocessing.Pool进程池创建的进程间进行通信
# def producer(queue):
#     queue.put('A')
#     time.sleep(2)
#
#
# def consumer(queue):
#     time.sleep(2)
#     data = queue.get()
#     print("consumer:%s" % data)
#
#
# if __name__ == '__main__':
#     # queue = Queue(10)  # 这个是使用multiprocessing.Queue,无效
#     queue = Manager().Queue(10)  # 这个是使用multiprocessing.Manager.Queue, 可以
#     pool = Pool(2)
#     pool.apply_async(producer, args=(queue,))
#     pool.apply_async(consumer, args=(queue,))
#     pool.close()
#     pool.join()


# 4.通过Pipe进行线程间的通信,  pipe进程间通信的性能高于Queue
# def producer(pipe):
#     pipe.send('admin')
#
#
# def consumer(pipe):
#     data = pipe.recv()
#     print("consumer:%s" % data)
#
#
# if __name__ == '__main__':
#     receive_pipe, send_pipe = Pipe()
#     """Pipe只能适应于两个进程间的通信"""
#     p = Process(target=producer, args=(send_pipe,))
#     c = Process(target=consumer, args=(receive_pipe,))
#     p.start()
#     c.start()
#     p.join()
#     c.join()


# 5. 进程间通信的其它方式

def add_data(p_dict, key, value):
    p_dict[key] = value


if __name__ == '__main__':
    progress_dict = Manager().dict()  #Manager()类中提供的数据结构都能够做到进程的通信
    first_progress = Process(target=add_data, args=(progress_dict, 'name', 'admin',))
    second_progress = Process(target=add_data, args=(progress_dict, 'age', 45,))
    first_progress.start()
    second_progress.start()
    first_progress.join()
    second_progress.join()
    print(progress_dict) #{'age': 45, 'name': 'admin'}

猜你喜欢

转载自www.cnblogs.com/z-qinfeng/p/12064529.html