Skip to content

Instantly share code, notes, and snippets.

@rystsov
Created March 16, 2013 17:38
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 rystsov/5177438 to your computer and use it in GitHub Desktop.
Save rystsov/5177438 to your computer and use it in GitHub Desktop.
Задачка Умпутума (Parallel order respectful task queue streaming processing)
from Queue import Queue
from threading import Thread
from time import sleep
# main queues
tasks = Queue()
results = Queue()
# auxiliary queues & workers ####################################################
preresults = Queue()
class Worker:
def __init__(self):
self.tasks = Queue()
self.results = Queue()
def __call__(self):
while True:
task = self.tasks.get()
if task==None: return
sleep(1) # emulates long task processing
self.results.put(task)
def startdispatcher(workers, tasks, preresults):
i = 0
while True:
task = tasks.get()
if task==None:
for worker in workers: worker.tasks.put(None)
preresults.put(None)
return
i = (i+1) % len(workers)
preresults.put(i)
workers[i].tasks.put(task)
def startpacker(workers, preresults, results):
while True:
i = preresults.get()
if i==None:
results.put(None)
return
results.put(workers[i].results.get())
def startworker():
worker = Worker()
worker.thread = Thread(target = worker)
worker.thread.start()
return worker
workers = map(lambda x: startworker(), range(0,10))
dispatcher = Thread(target = lambda: startdispatcher(workers, tasks, preresults))
packer = Thread(target = lambda: startpacker(workers, preresults, results))
for i in [dispatcher, packer]: i.start()
#################################################################################
# lets put task
for i in range(0,100): tasks.put(i)
tasks.put(None)
# and checks if they will be calculated in parallel (faster when 100s)
# and with respect to order (from 0 to 99)
while True:
result = results.get()
if result==None: break
print result
print "Done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment