本文實例講述了python使用Queue在多個子進程間交換數據的方法。分享給大家供大家參考。具體如下:
這里將Queue作為中間通道進行數據傳遞,Queue是線程和進程安全的
1
2
3
4
5
6
7
8
9
|
from multiprocessing import Process, Queue def f(q): q.put([ 42 , None , 'hello' ]) if __name__ = = '__main__' : q = Queue() p = Process(target = f, args = (q,)) p.start() print q.get() # prints "[42, None, 'hello']" p.join() |
希望本文所述對大家的Python程序設計有所幫助。