Last active
April 28, 2022 01:42
-
-
Save vsouza/537e932409a0f027e1d6 to your computer and use it in GitHub Desktop.
Python thread example.
This file contains 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
from Queue import Queue | |
from threading import Thread | |
class Worker(Thread): | |
"""Thread executing tasks from a given tasks queue""" | |
def __init__(self, tasks): | |
Thread.__init__(self) | |
self.tasks = tasks | |
self.daemon = True | |
self.start() | |
def run(self): | |
while True: | |
func, args, kargs = self.tasks.get() | |
try: func(*args, **kargs) | |
except Exception, e: print e | |
self.tasks.task_done() | |
class ThreadPool: | |
"""Pool of threads consuming tasks from a queue""" | |
def __init__(self, num_threads): | |
self.tasks = Queue(num_threads) | |
for _ in range(num_threads): Worker(self.tasks) | |
def add_task(self, func, *args, **kargs): | |
"""Add a task to the queue""" | |
self.tasks.put((func, args, kargs)) | |
def wait_completion(self): | |
"""Wait for completion of all the tasks in the queue""" | |
self.tasks.join() | |
if __name__ == '__main__': | |
from random import randrange | |
delays = [randrange(1, 10) for i in range(100)] | |
from time import sleep | |
def wait_delay(d): | |
print 'sleeping for (%d)sec' % d | |
sleep(d) | |
# 1) Init a Thread pool with the desired number of threads | |
pool = ThreadPool(20) | |
for i, d in enumerate(delays): | |
# print the percentage of tasks placed in the queue | |
print '%.2f%c' % ((float(i)/float(len(delays)))*100.0,'%') | |
# 2) Add the task to the queue | |
pool.add_task(wait_delay, d) | |
# 3) Wait for completion | |
pool.wait_completion() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment