欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python通過4種方式實(shí)現(xiàn)進(jìn)程數(shù)據(jù)通信

 更新時(shí)間:2020年03月12日 13:01:01   作者:我太難了008  
這篇文章主要介紹了Python通過4種方式實(shí)現(xiàn)進(jìn)程數(shù)據(jù)通信,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

python提供了4種方式來滿足進(jìn)程間的數(shù)據(jù)通信

1. 使用multiprocessing.Queue可以在進(jìn)程間通信,但不能在Pool池創(chuàng)建的進(jìn)程間進(jìn)行通信

2. 使用multiprocessing.Manager.Queue可以在Pool進(jìn)程池創(chuàng)建的進(jìn)程間進(jìn)行通信

3. 通過Pipe進(jìn)行線程間的通信, pipe進(jìn)程間通信的性能高于Queue,但是它只能在兩個(gè)進(jìn)程間進(jìn)行通信

4. 使用Manager類提供的數(shù)據(jù)結(jié)構(gòu)可以進(jìn)行進(jìn)程間的通信

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


# 1. 使用multiprocessing.Queue可以在進(jìn)程間通信

# 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. 使用共享全局變量,在多進(jìn)程間通信(結(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進(jìn)程池創(chuàng)建的進(jìn)程間進(jìn)行通信
# 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) # 這個(gè)是使用multiprocessing.Queue,無效
#   queue = Manager().Queue(10) # 這個(gè)是使用multiprocessing.Manager.Queue, 可以
#   pool = Pool(2)
#   pool.apply_async(producer, args=(queue,))
#   pool.apply_async(consumer, args=(queue,))
#   pool.close()
#   pool.join()


# 4.通過Pipe進(jìn)行線程間的通信, pipe進(jìn)程間通信的性能高于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只能適應(yīng)于兩個(gè)進(jìn)程間的通信"""
#   p = Process(target=producer, args=(send_pipe,))
#   c = Process(target=consumer, args=(receive_pipe,))
#   p.start()
#   c.start()
#   p.join()
#   c.join()


# 5. 進(jìn)程間通信的其它方式

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

if __name__ == '__main__':
  progress_dict = Manager().dict() #Manager()類中提供的數(shù)據(jù)結(jié)構(gòu)都能夠做到進(jìn)程的通信
  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'}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論