Skip to content

Instantly share code, notes, and snippets.

@Baekalfen
Created July 15, 2015 11:08
Show Gist options
  • Save Baekalfen/356218ca647929f39823 to your computer and use it in GitHub Desktop.
Save Baekalfen/356218ca647929f39823 to your computer and use it in GitHub Desktop.
Run parallel tasks in Python
# ParallelBatch.py
#
# Created by Mads Ynddal on 15/07/15.
# 2015 Printix.net
#
from multiprocessing.pool import ThreadPool
class batch:
processes = 4
pool = ThreadPool(processes = processes)
tasks = []
running = []
results = []
def add(self, task):
self.tasks.append(task)
def addList(self, taskList):
for t in taskList:
self.tasks.append(t)
def start(self):
for t in self.tasks:
r = self.pool.apply_async(t)
self.running.append(r)
for r in self.running:
self.results.append(r.get())
if __name__ == "__main__":
b = batch()
b.add(lambda: 2+2)
b.add(lambda: 3+2)
b.add(lambda: 4+2)
b.add(lambda: 5+2)
b.start()
print b.results
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment