Skip to content

Instantly share code, notes, and snippets.

@harlowja
Created June 19, 2014 03:42
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 harlowja/e7b2eede63e2b9510214 to your computer and use it in GitHub Desktop.
Save harlowja/e7b2eede63e2b9510214 to your computer and use it in GitHub Desktop.
Slow queue workers
import time
import threading
import sloq
THREADS = 5
RATE = 1
WORK_UNITS = 100
WORK_DELAY = 0.1
def do_stuff(name, q):
running = True
while running:
w = q.get()
if w is None:
running = False
q.task_done()
else:
try:
now = time.time()
print("%s %s doing work %s" % (now, name, w))
time.sleep(WORK_DELAY)
now = time.time()
print("%s %s finished work %s" % (now, name, w))
finally:
q.task_done()
def main():
q = sloq.SlowQueue(release_tick=RATE)
for i in range(0, WORK_UNITS):
q.put(i + 1)
for i in range(0, THREADS):
q.put(None)
for i in range(0, THREADS):
name = "thread_%s" % (i + 1)
t = threading.Thread(target=do_stuff, args=(name, q), name=name)
t.daemon = True
t.start()
q.join()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment