SHARE
TWEET

Untitled

a guest Sep 15th, 2019 106 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # example of threads working on a queue
  2.  
  3. from Queue import Queue
  4. from threading import Thread
  5.  
  6. num_worker_threads = 10
  7.  
  8. # items to work on
  9. def source():
  10.     return xrange(400)
  11.  
  12. # the actual work to be done
  13. def do_work(smth):
  14.     print("{}\r".format(smth))
  15.  
  16. # worker that retrieves data from queue and executes work
  17. def worker():
  18.     while True:
  19.         item = q.get()
  20.         do_work(item)
  21.         q.task_done()
  22.  
  23. q = Queue()
  24. for i in range(num_worker_threads):
  25.      t = Thread(target=worker)  #use args to pass args
  26.      t.daemon = True
  27.      t.start()
  28.  
  29. # queues items to work on
  30. for item in source():
  31.     q.put(item)
  32.  
  33. # blocks until work is finished
  34. q.join()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top