Created
February 21, 2019 17:18
-
-
Save arthuralvim/356795612606326f08d99c6cd375f796 to your computer and use it in GitHub Desktop.
Example of python queues and multithreading.
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
import queue | |
import threading | |
num_worker_threads = 1 | |
def do_work(item): | |
print(item) | |
def source(): | |
return range(100) | |
def worker(): | |
while True: | |
item = q.get() | |
if item is None: | |
break | |
do_work(item) | |
q.task_done() | |
q = queue.Queue() | |
threads = [] | |
for i in range(num_worker_threads): | |
t = threading.Thread(target=worker) | |
t.start() | |
threads.append(t) | |
for item in source(): | |
q.put(item) | |
# block until all tasks are done | |
q.join() | |
print('stopping workers!') | |
# stop workers | |
for i in range(num_worker_threads): | |
q.put(None) | |
for t in threads: | |
t.join() |
This is great, but queue is a global...
What if I have a long running thread1
Thread 2 is a consumer of thread 1 that runs intermittently, but thread 1 is always running/filling its queue.
How do you start thread2 and pass the queue object so that it can access it? (i'm on django, maybe memcache?)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It's perfect. Thank you