博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
a threadpool by python
阅读量:6327 次
发布时间:2019-06-22

本文共 3830 字,大约阅读时间需要 12 分钟。

from 

#!/usr/bin/python# -*- coding: utf-8 -*-"""A simple thread pool.@author: Junaid P V@license: GPLv3"""    from threading import Thread, RLock, Lockfrom time import sleepfrom functools import wrapsdef synchronous( tlockname ):    """    A decorator to place an instance based lock around a method    from: http://code.activestate.com/recipes/577105-synchronization-decorator-for-class-methods/    """    def _synched(func):        @wraps(func)        def _synchronizer(self,*args, **kwargs):            tlock = self.__getattribute__( tlockname)            tlock.acquire()            try:                return func(self, *args, **kwargs)            finally:                tlock.release()        return _synchronizer    return _synchedclass ThreadPoolThread(Thread):        def __init__(self, pool):        Thread.__init__(self)        self.__pool = pool        self.start()    def run(self):        try:            while True:                task = self.__pool.pop_task()                if task == None:                    break                if task[1] != None and task[2] != None:                    task[0](*task[1], **task[2])                elif task[1] != None:                    task[0](*task[1])                else:                    task[0]()        finally:            # Always inform about thread finish            self.__pool.thread_finished()    class ThreadPool(object):        def __init__(self, thread_count):        self.__tasks = []        self.task_lock = Lock()        self.__thread_count = thread_count        self.__threads = []        self.threads_lock = Lock()        self.__finished_threads_count = 0        self.finished_threads_count_lock = Lock()            @synchronous('task_lock')    def add_task(self, callable_, args=None, kwds=None):        self.__tasks.append((callable_, args, kwds))        @synchronous('task_lock')    def pop_task(self):        if len(self.__tasks) > 0:            return self.__tasks.pop(0)        else:            return None        def start_workers(self):        self.__finished_threads_count = 0        self.__threads = []        for i in range(self.__thread_count):            worker = ThreadPoolThread(self)            self.__threads.append(worker)        def wait(self):        """        Wait for every worker threads to finish        """        while True:            finished_threads_count = self.get_finished_threads_count()            if finished_threads_count == self.__thread_count:                break            sleep(1)        @synchronous('finished_threads_count_lock')    def thread_finished(self):        self.__finished_threads_count += 1        @synchronous('finished_threads_count_lock')    def get_finished_threads_count(self):        return self.__finished_threads_count"""Example"""if __name__ == '__main__':    from threading import current_thread    class SampleTask:        def __init__(self, name):            self.name = name        def call_me(self, count):            print "Thread: ", current_thread().getName()            for i in range(count):                print self.name, ': counting ', i                sleep(3)        pool = ThreadPool(2)    a = SampleTask("A")    b = SampleTask("B")    c = SampleTask("C")    d = SampleTask("D")    e = SampleTask("E")    pool.add_task(a.call_me, (5,))    pool.add_task(b.call_me, (7,))    pool.add_task(c.call_me, (6,))    pool.add_task(d.call_me, (4,))    pool.add_task(e.call_me, (5,))    pool.start_workers()    pool.wait()    print "Sleeping 5 seconds before next run..."    sleep(5)    f = SampleTask("F")    g = SampleTask("G")    h = SampleTask("H")    pool.add_task(f.call_me, (5,))    pool.add_task(g.call_me, (4,))    pool.add_task(h.call_me, (3,))    pool.start_workers()    pool.wait()

  理解线程池大概如何实现,然后改写成C的。

转载于:https://www.cnblogs.com/lxgeek/archive/2011/08/09/2132806.html

你可能感兴趣的文章
华为交换机802.1x动态下发vlan配置
查看>>
spring boot websocket + thy模版
查看>>
查看文件的真实路径
查看>>
如何开发一个自己的 RubyGem?
查看>>
centos 7 修改主机名的方法
查看>>
WSUS系列之二:WSUS角色安装
查看>>
大数据作业第4天
查看>>
职工系统150206308
查看>>
『中级篇』K8S最小调度单位Pod(62)
查看>>
我的友情链接
查看>>
ACE网络编程思考(一)
查看>>
数据结构的几种存储方式
查看>>
React源码学习——ReactClass
查看>>
JavaScript中几个相似方法对比
查看>>
如何恢复RAWD盘的资料
查看>>
物联网+云平台未来方向之一
查看>>
大作业项目
查看>>
Red Hat Linux、rhel和Fedora Core以及Centos区别与联系
查看>>
配置Apache、Php、Mysql
查看>>
Java基础---Java常量的应用(九)
查看>>