Last active
November 12, 2015 17:55
-
-
Save cevaris/7c0c421d20b6c78c3f3d to your computer and use it in GitHub Desktop.
Simple Thread Pool Execution in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python -u | |
import itertools | |
import sys | |
import time | |
import thread | |
import threading | |
import random | |
from multiprocessing.dummy import Pool as ThreadPool | |
class DummyThreadPool(object): | |
def __init__(self, num_threads=1): | |
self.pool = ThreadPool(num_threads) | |
self.results = None | |
def submit(self, func, data): | |
return self.pool.map(func, data) | |
def wait(self): | |
self.pool.close() | |
self.pool.join() | |
# Usage | |
def do_work(arg): | |
thread_id = thread.get_ident() | |
print '{} is starting work on {}'.format(thread_id, arg) | |
time.sleep(random.randint(1,10)) | |
print '{} is done working on {}'.format(thread_id, arg) | |
return arg*2 | |
pool = DummyThreadPool(15) | |
results = pool.submit(do_work, range(1,20)) | |
pool.wait() | |
print results |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment