当前位置: 首页>>代码示例>>Python>>正文


Python ThreadPool.add方法代码示例

本文整理汇总了Python中threadpool.ThreadPool.add方法的典型用法代码示例。如果您正苦于以下问题:Python ThreadPool.add方法的具体用法?Python ThreadPool.add怎么用?Python ThreadPool.add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在threadpool.ThreadPool的用法示例。


在下文中一共展示了ThreadPool.add方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: run

# 需要导入模块: from threadpool import ThreadPool [as 别名]
# 或者: from threadpool.ThreadPool import add [as 别名]
    def run(self, env, json_writer):
        '''
        Schedule all tests in profile for execution.

        See ``Test.schedule`` and ``Test.run``.
        '''

        self.prepare_test_list(env)

        # If using concurrency, add all the concurrent tests to the pool and
        # execute that pool
        if env.concurrent:
            pool = ThreadPool(multiprocessing.cpu_count())
            for (path, test) in self.test_list.items():
                if test.runConcurrent:
                    pool.add(test.execute, (env, path, json_writer))
            pool.join()

        # Run any remaining tests serially from a single thread pool after the
        # concurrent tests have finished
        pool = ThreadPool(1)
        for (path, test) in self.test_list.items():
            if not env.concurrent or not test.runConcurrent:
                pool.add(test.execute, (env, path, json_writer))
        pool.join()
开发者ID:RAOF,项目名称:piglit,代码行数:27,代码来源:core.py

示例2: __init__

# 需要导入模块: from threadpool import ThreadPool [as 别名]
# 或者: from threadpool.ThreadPool import add [as 别名]
class ThriftThreadServer:
    def __init__(self, module, handler_class, addr, max_process=1, max_conn=50, framed=True):
        module.handler = handler_class()
        global service
        service = module
        self.framed = framed

        self.workers = []
        self.running = True

        def signal_master_handler(signum, frame):
            log.warn("signal %d catched in master %d, wait for kill all worker", signum, os.getpid())
            self.running = False
            for p in self.workers:
                p.terminate()
        
        def signal_worker_handler(signum, frame):
            log.warn("worker %d will exit after all request handled", os.getpid())
            #self.server.close()
            self.running = False
            self.sock.close()

        def server_init():
            signal.signal(signal.SIGTERM, signal_worker_handler)
            log.warn('server started addr=%s:%d pid=%d', addr[0], addr[1], os.getpid())
            if hasattr(service.handler, '_initial'):
                service.handler._initial()
            #self.server.serve_forever()

        def _start_process(index):
            server_name = 'proc-%02d' % index
            p = multiprocessing.Process(target=server_start, name=server_name)
            p.start()
            return p
        
        def signal_child_handler(signum, frame):
            time.sleep(1)
            if self.running:
                log.warn("master recv worker exit, fork one")
                try:
                    pinfo = os.waitpid(-1, 0)
                    pid = pinfo[0]

                    index = -1
                    for i in range(0, len(self.workers)):
                        p = self.workers[i]
                        if p.pid == pid:
                            index = i
                            break
                   
                    if index >= 0:
                        self.workers[index] = _start_process(index)
                except OSError:
                    log.info('waitpid error:')

        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
        self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.sock.bind(addr)
        self.sock.listen(1024)

        server_init()

        self.tp = ThreadPool(max_conn, max_conn*5) 
        self.tp.start()


    def forever(self):
        class MyTask (Task):
            def run(self):
                return self._func(*self._args, **self._kwargs)

        while self.running:
            try:
                client, addr = self.sock.accept()
                self.tp.add(MyTask(handle, client=client, addr=addr, framed=self.framed))
            except KeyboardInterrupt:
                break
                #os.kill(os.getpid(), 9)
            except Exception, e:
                log.warn(traceback.format_exc())
                log.warn('master exception: %s', str(e))

        self.sock.close()
        self.sock = None
        self.tp.stop_wait()

        log.warn('master exit')
开发者ID:zhaoweikid,项目名称:zbase,代码行数:89,代码来源:thriftserver.py


注:本文中的threadpool.ThreadPool.add方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。