Created
September 4, 2019 18:41
-
-
Save DataGreed/922194d6a259bede82190d473be678fd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# example of threads working on a queue | |
from Queue import Queue | |
from threading import Thread | |
num_worker_threads = 10 | |
# items to work on | |
def source(): | |
return xrange(400) | |
# the actual work to be done | |
def do_work(smth): | |
print("{}\r".format(smth)) | |
# worker that retrieves data from queue and executes work | |
def worker(): | |
while True: | |
item = q.get() | |
do_work(item) | |
q.task_done() | |
q = Queue() | |
for i in range(num_worker_threads): | |
t = Thread(target=worker) #use args to pass args | |
t.daemon = True | |
t.start() | |
# queues items to work on | |
for item in source(): | |
q.put(item) | |
# blocks until work is finished | |
q.join() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment