Skip to content

Instantly share code, notes, and snippets.

@meganlkm
Last active May 21, 2020 19:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meganlkm/dce2d2ff814ad852250783c25e3a539e to your computer and use it in GitHub Desktop.
Save meganlkm/dce2d2ff814ad852250783c25e3a539e to your computer and use it in GitHub Desktop.
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