Python: Multithreading does not run -
i trying run multithreaded code in python as
queue import queue q = queue() doclist=["adsas","asdasd","oipoipo"] i,doc in enumerate(doclist): q.put(doc) q.join() threadrun.run(50, qworker.worker(q)) first, create queue , add stuff it. then, call method creates , runs threads. here threadrun.run method
import threading def run(numthreads,targetmethod): print "running threads" in range(numthreads): t = threading.thread(target=targetmethod) t.daemon=true t.start() and here qworker.worker method
def worker(qitem): print "q worker" while true: doc = qitem.get() try: print doc qitem.task_done() except: print "error" when execute above code, nothing happens. approach correct? missing?
you calling join on queue before starting threads code block there.
so start threads run q.join()
edit
one more error passing of queue, this:
threadrun.run(50, qworker.worker(q)) should be:
threadrun.run(50, qworker.worker, q) so add queue parameter run function , when create threads do:
t = threading.thread(target=targetmethod, args=(q,)) the reason saw "q worker" output because called worker function here: threadrun.run(50, qworker.worker(q))
Comments
Post a Comment