我有兩個進程,一個將作業添加到隊列中,另一個將它們從同一隊列中取出並運行它們。這應該按預期工作,我不知道爲什麼worker
從未得到任何工作。這裏是我的代碼:如何在Python中的兩個進程之間共享一個變量?
from multiprocessing import Process
from Queue import Queue
import time
q = Queue()
def queuer():
while True:
q.put("JOB")
print "Adding JOB"
time.sleep(1)
def worker():
while True:
if not q.empty():
item = q.get()
print "Running", item
else:
print "No jobs"
time.sleep(1)
a = Process(target=queuer)
a.start()
b = Process(target=worker)
b.start()
每個進程都有它自己的隊列副本。有另一個stackoverflow線程討論這個:http://stackoverflow.com/questions/11109776/changing-global-variable-when-multiprocessing-in-python – wizard23