zoukankan      html  css  js  c++  java
  • Python之路:线程池

    版本一

    #!/usr/bin/env  python
    #
     --*--coding:utf-8 --*--
    import Queue
    import threading


    class ThreadPool(object):
    #创建类
        def __init__(self, max_num=20):#进程函数,默认最大20个进程
            self.queue = Queue.Queue(max_num)#生成进程
            for i in xrange(max_num):#循环进程
                self.queue.put(threading.Thread)#上传进程

        def get_thread(self):#下载进程函数
            return self.queue.get()

        def add_thread(self):#生成进程函数
            self.queue.put(threading.Thread)


    pool = ThreadPool(10)#执行类,并传默认进程数值

    def func(arg, p):#打印进程
        print arg
        import time
        time.sleep(2)#间隔2秒
        p.add_thread()


    for i in xrange(30):#循环进程
        thread = pool.get_thread()
        t = thread(target=func, args=(i, pool))#传值到func函数,并且执行
        t.start()

     版本二

    from Queue import Queue
    import contextlib
    import threading
     
    WorkerStop = object()
     
     
    class ThreadPool:
     
        workers = 0
     
        threadFactory = threading.Thread
        currentThread = staticmethod(threading.currentThread)
     
        def __init__(self, maxthreads=20, name=None):
     
            self.q = Queue(0)
            self.max = maxthreads
            self.name = name
            self.waiters = []
            self.working = []
     
        def start(self):
            while self.workers < min(self.max, self.q.qsize()+len(self.working)):
                self.startAWorker()
     
        def startAWorker(self):
            self.workers +
    = 1
            
    name = "PoolThread-%s-%s" % (self.name or id(self), self.workers)
            newThread 
    = self.threadFactory(target=self._worker, name=name)
            
    newThread.start()
     
        def callInThread(self, func, *args, **kw):
            self.callInThreadWithCallback(None, func, *args, **kw)
     
        def callInThreadWithCallback(self, onResult, func, *args, **kw):
            o 
    = (func, args, kw, onResult)
            self.q.put(o)
     
     
        @contextlib.contextmanager
        def _workerState(self, stateList, workerThread):
            stateList.append(workerThread)
            try:
                yield
            finally:
                stateList.remove(workerThread)
     
        def _worker(self):
            ct 
    = self.currentThread()
            
    = self.q.get()
            
    while o is not WorkerStop:
                with self._workerState(self.working, ct):
                    function, args, kwargs, onResult 
    = o
                    
    del o
                    try:
                        result 
    = function(*args, **kwargs)
                        success 
    = True
                    
    except:
                        success 
    = False
                        
    if onResult is None:
                            pass
     
                        else:
                            pass
     
                    del function, args, kwargs
     
                    if onResult is not None:
                        try:
                            onResult(success, result)
                        except:
                            #context.call(ctx, log.err)
                            pass
     
                    del onResult, result
     
                with self._workerState(self.waiters, ct):
                    o 
    = self.q.get()
     
        
    def stop(self):
            while self.workers:
                self.q.put(WorkerStop)
                self.workers -
    = 1
     
     
    """
    def show(arg):
        import time
        time.sleep(1)
        print arg
     
     
    pool 
    = ThreadPool(20)
     
    for i in range(500):
        pool.callInThread(show, i)
     
    pool.start()
    pool.stop()
    """
  • 相关阅读:
    java 实现前面带零的数字进行加减运算(保留前面的0)
    java 判断字符串是否是整数(纯数字:正整数、负整数、0)、至少包含一位小数、数字(可以是整数或小数)
    java 抽象类使用@Autowrited注入对象,子类直接使用父类的该属性
    java、springboot使用proguard混淆代码
    idea 使用阿里云仓库下载的jar包出现证书校验问题(PKIX:unable to find valid certification path to requested target)
    windows10安装zookeeper-3.6.2并生成zookeeper服务
    zookeeper-3.4.8 集群搭建
    zookeeper安装和使用 windows环境
    Dubbo入门---搭建一个最简单的Demo框架
    服务端高并发分布式架构演进之路
  • 原文地址:https://www.cnblogs.com/wulaoer/p/5122472.html
Copyright © 2011-2022 走看看