Skip to content

Instantly share code, notes, and snippets.

@haxrob
Created November 27, 2018 01:35
Show Gist options
  • Save haxrob/d8ca3d7cd88110163162b4f1dfc42ecf to your computer and use it in GitHub Desktop.
Save haxrob/d8ca3d7cd88110163162b4f1dfc42ecf to your computer and use it in GitHub Desktop.
import Queue
import random
import string
import time
import Queue
from threading import Thread, Event
import signal
q = Queue.Queue()
threads = []
def do() :
global q
y = 1
while y <= 3 :
print "round:", y
z = 1
while z <= 5 :
N = 5
x = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(N))
time.sleep(0.10)
q.put(x)
z = z + 1
time.sleep(0.5)
y = y + 1
class Worker(Thread) :
def __init__(self, q) :
Thread.__init__(self)
self.signal = Event()
self.q = q
def run(self) :
print "Thread #%s started" % self.ident
while not self.signal.is_set() :
try :
v = self.q.get(False)
print v
self.q.task_done()
except Queue.Empty :
pass
print "Thread #%s stopped" % self.ident
def end_jobs() :
global threads
for t in threads :
t.signal.set()
for t in threads :
t.join()
def shutdown(signum, frame) :
print 'Caught signal %d' % signum
end_jobs()
def main() :
global threads
signal.signal(signal.SIGTERM, shutdown)
signal.signal(signal.SIGINT, shutdown)
workers = 5
for i in range(workers) :
t = Worker(q)
t.start()
threads.append(t)
do()
#q.join()
print "Exiting!"
end_jobs()
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment