1

I am trying to run some multithreaded code in python as

    from Queue import Queue 
    q = Queue()
    doclist=["adsas","asdasd","oipoipo"]
    for i,doc in enumerate(doclist):
        q.put(doc)
    q.join()

    threadRun.run(50, qWorker.worker(q))

first, i create a queue and add some stuff to it. Then, i call a method which creates and runs the threads. Here is the threadRun.run method

import threading
def run(numThreads,targetMethod):
    print "Running threads"
    for i in range(numThreads):
        t = threading.Thread(target=targetMethod)
        t.daemon=True
        t.start()

and here is the qWorker.worker method

def worker(qItem):
    print "Q Worker"
    while True:
            doc = qItem.get()

            try:
                print doc
                qItem.task_done()
            except:
                print "Error"

When i execute the above code, nothing happens. Is my approach correct? what am i missing?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
AbtPst
  • 7,778
  • 17
  • 91
  • 172
  • 1
    Nothing happens? It doesn't print "Running threads"? – OneCricketeer Feb 08 '16 at 19:14
  • is `qWorker.worker(q)` a callable object? I think, before calling `run` python trying to calculate the value of `qWorker.worker(q)` for passing parameters to thread use `args` parameter of `threading.Thread()` class threading.Thread(group=None, target=None, name=None, args=(), kwargs={}, *, daemon=None) – steel.ne Feb 08 '16 at 19:25
  • Is your code in a `__main__` block? What os are you on? – Peter Wood Feb 08 '16 at 19:31
  • no, it does not print "Running threads" – AbtPst Feb 08 '16 at 19:34
  • yes, my code is in a __main__ block. i am on windows7. i have python 2.7 through anaconda – AbtPst Feb 08 '16 at 19:35

1 Answers1

5

You are calling join on the queue before starting the threads so your code will block there.

So start the threads then run q.join()

EDIT

One more error is the passing of the queue, this:

 threadRun.run(50, qWorker.worker(q))

should be:

 threadRun.run(50, qWorker.worker, q)

So add the queue as a parameter to the run function and when you create the threads do:

t = threading.Thread(target=targetMethod, args=(q,))

The reason you saw the "Q worker" output was because you actually called the worker function here: threadRun.run(50, qWorker.worker(q))

Fredrik
  • 1,389
  • 1
  • 14
  • 32