Python Simple Thread Pool

Python
2015-10-16 16:35 (9 years ago) ytyng

I should have used multiprocessing.pool.Pool orz

import time
import threading
from queue import Queue


class ThreadPool(object):
    def __init__(self, pool_size=10):
        self.pool_size = pool_size
        self.jobs = []
        self.job_queue = Queue()
        self.result_queue = Queue()

    def add_job(self, job):
        self.jobs.append(job)

    def run(self):
        def worker():
            while True:
                job = self.job_queue.get()
                self.result_queue.put(job())
                self.job_queue.task_done()

        for i in range(self.pool_size):
            t = threading.Thread(target=worker)
            t.daemon = True
            t.start()

        for job in self.jobs:
            self.job_queue.put(job)

        self.job_queue.join()

    def results(self):
        while not self.result_queue.empty():
            yield self.result_queue.get()


def demo():
    def demo_job(job_id):
        def _job():
            print('Start:', job_id)
            time.sleep(1)
            print('End:', job_id)
            return 'Return: {}'.format(job_id)

        return _job

    thread_pool = ThreadPool(4)

    for i in range(10):

        task = demo_job(i)
        thread_pool.add_job(task)

    thread_pool.run()

    for i in thread_pool.results():
        print(i)


if __name__ == '__main__':
    demo()

Execution Result

Start: 0
Start: 1
Start: 2
Start: 3
End: 0
End: 1
Start: 4
End: 3
Start: 5
Start: 6
End: 2
Start: 7
End: 4
Start: 8
End: 7
Start: 9
End: 5
End: 6
End: 8
End: 9
Return: 1
Return: 3
Return: 0
Return: 2
Return: 4
Return: 7
Return: 5
Return: 6
Return: 8
Return: 9

gist: https://gist.github.com/ytyng/a3776b725e9ea771bca4

Currently unrated

Comments

Archive

2024
2023
2022
2021
2020
2019
2018
2017
2016
2015
2014
2013
2012
2011