Last active
May 21, 2020 19:21
-
-
Save meganlkm/dce2d2ff814ad852250783c25e3a539e 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
import queue | |
from threading import Thread | |
class Worker(Thread): | |
def __init__(self, q, other_arg, *args, **kwargs): | |
self.q = q | |
self.other_arg = other_arg | |
super().__init__(*args, **kwargs) | |
def run(self): | |
while True: | |
try: | |
work = self.q.get(timeout=3) # 3s timeout | |
print(f"work: {work}") | |
except queue.Empty: | |
print("The queue is empty") | |
return | |
# do whatever work you have to do on work | |
print(f"other_arg: {self.other_arg}") | |
self.q.task_done() | |
q = queue.Queue() | |
for ptf in range(20): | |
q.put_nowait(ptf) | |
for i in range(20): | |
Worker(q, i).start() | |
# blocks until the queue is empty. | |
q.join() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment