• Python之线程池


    线程池

    实例一:

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    import Queue
    import threading
     
     
    class ThreadPool(object):
     
        def __init__(self, max_num=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)
        p.add_thread()
     
     
    for i in xrange(30):
        thread = pool.get_thread()
        t = thread(target=func, args=(i, pool))
        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()
            o = 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()
    """

    更多参见:twisted.python.threadpool

    上下文管理:https://docs.python.org/2/library/contextlib.html

    参考:http://www.cnblogs.com/wupeiqi/articles/4839959.html

  • 相关阅读:
    Delphi 实现任务栏多窗口图标显示
    Win7如何部署定制的Quicklaunch图标
    Delphi中关于菜单的几个技巧
    delphi里为程序任务栏右键菜单添加自定义菜单
    DELPHI 让子窗体显示在任务栏上
    C# Newtonsoft.Json 读取文件,返回json字符串
    C# Newtonsoft.Json 读取文件,返回json字符串
    在使用layui Table时,死活显示不了数据,无效的 JSON 基元 解决办法
    newtonsoft返回json去掉字符串
    ASP.NET MVC AJAX 请求中加入 antiforgerytoken 解决“所需的防伪表单字段“__RequestVerificationToken”不存在”问题
  • 原文地址:https://www.cnblogs.com/fanweibin/p/5121238.html
Copyright © 2020-2023  润新知