擅长:python、mysql、java
<p>两件事:</p>
<ol>
<li>您需要将队列作为参数传递给两个进程。在</li>
<li>你应该用多处理队列,不是排队。排队(用于螺纹)</li>
</ol>
<p>这个代码适用于我:</p>
<pre><code>from multiprocessing import Process, Queue
import time
def queuer(q):
while True:
q.put("JOB")
print "Adding JOB"
time.sleep(1)
def worker(q):
while True:
if not q.empty():
item = q.get()
print "Running", item
else:
print "No jobs"
time.sleep(1)
if __name__ == '__main__':
q = Queue()
a = Process(target=queuer, args=(q,))
b = Process(target=worker, args=(q,))
a.start()
b.start()
</code></pre>