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


Python ThreadPool.callInThread方法代码示例

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


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

示例1: AddressResolver

# 需要导入模块: from twisted.python.threadpool import ThreadPool [as 别名]
# 或者: from twisted.python.threadpool.ThreadPool import callInThread [as 别名]
class AddressResolver(object):
    pool = None

    def __init__(self, minthreads=1, maxthreads=4):
        self.pool = ThreadPool(minthreads=minthreads, maxthreads=maxthreads)
        # unclosed ThreadPool leads to reactor hangs at shutdown
        # this is a problem in many situation, so better enforce pool stop here
        reactor.addSystemEventTrigger(
            "before", "shutdown", self.pool.stop
        )

        self.pool.start()

    def get_host_by_name(self, address):
        d = defer.Deferred()

        def func():
            try:
                reactor.callFromThread(
                    d.callback, socket.gethostbyname(address)
                )
            except Exception as e:
                reactor.callFromThread(d.errback, e)

        self.pool.callInThread(func)
        return d

    def close(self):
        self.pool.stop()
开发者ID:UltrosBot,项目名称:Ultros,代码行数:31,代码来源:resolver.py

示例2: pylabsTaskletRunner

# 需要导入模块: from twisted.python.threadpool import ThreadPool [as 别名]
# 或者: from twisted.python.threadpool.ThreadPool import callInThread [as 别名]
class pylabsTaskletRunner(TaskletRunner):
    def __init__(self, engine, threadpoolsize=10):
        self.engine = engine
        # Job queue
        self._queue = Queue.Queue()
        # Threadpool
        self._runners = list()
        self._threadpool = None

        reactor.addSystemEventTrigger('after', 'startup', self.start,
                                      threadpoolsize)
        reactor.addSystemEventTrigger('before', 'shutdown',
                                      self.shutdown)

    def start(self, threadpoolsize):
        self._threadpool = ThreadPool(minthreads=threadpoolsize,
                                      maxthreads=threadpoolsize + 1)

        # Set up threadpool
        q.logger.log('[PMTASKLETS] Constructing taskletserver threadpool', 6)
        self._threadpool.start()
        for i in xrange(threadpoolsize):
            runner = TaskletRunnerThread(self._queue)
            self._runners.append(runner)
            self._threadpool.callInThread(runner.run)

        self._running = True


    def queue(self, params, author=None, name=None, tags=None, priority=-1,
              logname=None):
        author = author or '*'
        name = name or '*'
        tags = tags or list()
        priority = priority if priority > -1 else -1

        q.logger.log('[PMTASKLETS] Queue: params=%s, author=%s, name=%s, '
                     'tags=%s, priority=%d' % \
                     (params, author, name, tags, priority), 4)

        # Wrap the tasklet executor methods so the appname (for logging) is set
        # correctly
        def logwrapper(func):
            @functools.wraps(func)
            def _wrapped(*args, **kwargs):
                import pylabs

                oldappname = pylabs.q.application.appname
                if logname:
                    pylabs.q.application.appname = \
                            'applicationserver:pmtasklets:%s' % logname
                else:
                    pylabs.q.application.appname = \
                            'applicationserver:pmtasklets'

                try:
                    ret = func(*args, **kwargs)
                finally:
                    pylabs.q.application.appname = oldappname

                return ret

            return _wrapped

        execute_args = {
            'author': author,
            'name': name,
            'tags': tags,
            'priority': priority,
            'params': params,
            'wrapper': logwrapper,
        }

        #Append list of tasklet methods to run to the queue
        self._queue.put((self.engine, execute_args, ))

    def shutdown(self):
        q.logger.log('Shutting down tasklet runner', 5)
        self._running = False

        #Tell all threads to stop running
        for runner in self._runners:
            runner.keep_running = False

        self._threadpool.stop()

    @classmethod
    def install(cls):
        log.msg('Installing pylabs tasklet runner')
        import applicationserver

        applicationserver.TaskletRunner = cls
开发者ID:racktivity,项目名称:ext-pylabs-core,代码行数:94,代码来源:pylabs_bindings.py


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