2

I have two processes, one adds jobs to a queue and the other takes them off the same queue and runs them. This should work as expected and I'm not sure why the worker never gets any jobs. Here's my code:

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()
Velimir Mlaker
  • 10,664
  • 4
  • 46
  • 58
nickponline
  • 25,354
  • 32
  • 99
  • 167
  • each process has its own copy of the queue. There is another stackoverflow thread that discusses this: http://stackoverflow.com/questions/11109776/changing-global-variable-when-multiprocessing-in-python – wizard23 Mar 12 '14 at 23:46

2 Answers2

8

Two things:

  1. You need to pass the Queue as an argument to both processes.
  2. You should use multiprocessing.Queue, not Queue.Queue (which are for Threads)

This code works for me:

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()
Felix
  • 2,064
  • 3
  • 21
  • 31
1

One possibility is to use the Queue object from the multiprocessing namespace. It's described here: http://docs.python.org/2/library/multiprocessing.html#sharing-state-between-processes

So I adapted your code. I only made 2 changes: - Use multiprocessing Queue - Avoid globals and pass the queue as a parameter to the worker and queuer (this is not needed but it's good practice to keep everything tidy)

# use the Queue from the multiprocessing namespace!
from multiprocessing import Process, Queue
import time

q = Queue()

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)

a = Process(target=queuer, args =(q,))
a.start()

b = Process(target=worker, args = (q,))
b.start()
wizard23
  • 191
  • 2
  • 4