Skip to content

Instantly share code, notes, and snippets.

@tspycher
Created March 7, 2017 09:35
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 tspycher/0842bd4648f6dc5054e250c4abee57f3 to your computer and use it in GitHub Desktop.
Save tspycher/0842bd4648f6dc5054e250c4abee57f3 to your computer and use it in GitHub Desktop.
Simple Multithreaded Synchronisation routine
#!/usr/bin/python
import time
import threading
import Queue
import random
class BaseSynchronisation(object):
num_threads = 5
threads = None
queue = None
processing_state = None
def __init__(self, **kwargs):
super(BaseSynchronisation, self).__init__()
self.queue = Queue.Queue()
self.threads = []
self.processing_state = 1
for i in range(self.num_threads):
t = threading.Thread(target=self._getWork)
self.threads.append(t)
t.start()
def waitTillFinished(self):
self.queue.join()
self.processing_state = 0
for t in self.threads:
t.join()
def _doWork(self, work):
raise NotImplementedError()
def _getWork(self):
while self.processing_state == 1:
if not self.queue.empty():
work = self.queue.get()
self._doWork(work)
self.queue.task_done()
def _sync(self):
raise NotImplementedError()
@classmethod
def sync(cls, **kwargs):
s = cls(**kwargs)
s._sync()
class Service(BaseSynchronisation):
def _doWork(self, work):
print "Working on %d" % work
time.sleep(random.randint(1,5))
def _sync(self):
for i in range(0,20):
self.queue.put(i)
self.waitTillFinished()
print "done"
start = time.time()
Service.sync()
print "Execution took %.2f" % (time.time() - start)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment