Python通過4種方式實現(xiàn)進程數(shù)據(jù)通信
python提供了4種方式來滿足進程間的數(shù)據(jù)通信
1. 使用multiprocessing.Queue可以在進程間通信,但不能在Pool池創(chuàng)建的進程間進行通信
2. 使用multiprocessing.Manager.Queue可以在Pool進程池創(chuàng)建的進程間進行通信
3. 通過Pipe進行線程間的通信, pipe進程間通信的性能高于Queue,但是它只能在兩個進程間進行通信
4. 使用Manager類提供的數(shù)據(jù)結(jié)構(gòu)可以進行進程間的通信
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. 使用共享全局變量,在多進程間通信(結(jié)論: 不行)
# 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進程池創(chuàng)建的進程間進行通信
# 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()類中提供的數(shù)據(jù)結(jié)構(gòu)都能夠做到進程的通信
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'}
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
pytorch中部分矩陣乘法和數(shù)組乘法的小結(jié)
本文主要介紹了pytorch中部分矩陣乘法和數(shù)組乘法的小結(jié),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-03-03
Python Vaex實現(xiàn)快速分析100G大數(shù)據(jù)量
Vaex是一個開源的DataFrame庫,它可以對表格數(shù)據(jù)集進行可視化、探索、分析,甚至機器學習,這些數(shù)據(jù)集和你的硬盤驅(qū)動器一樣大。本文就來聊聊如何利用Vaex實現(xiàn)快速分析100G大數(shù)據(jù)量,需要的可以參考一下2023-03-03
Python統(tǒng)計字符內(nèi)容的占比的實現(xiàn)
本文介紹了如何使用Python統(tǒng)計字符占比,包括字符串中字母、數(shù)字、空格等字符的占比,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-08-08
用Python實現(xiàn)定時備份Mongodb數(shù)據(jù)并上傳到FTP服務器
這篇文章主要介紹了用Python實現(xiàn)定時備份Mongodb數(shù)據(jù)并上傳到FTP服務器,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-01-01

