• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Python pulsar.Future类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中pulsar.Future的典型用法代码示例。如果您正苦于以下问题:Python Future类的具体用法?Python Future怎么用?Python Future使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



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

示例1: test_coroutine1

 def test_coroutine1(self):
     loop = get_event_loop()
     d1 = Future()
     loop.call_later(0.2, d1.set_result, 1)
     a = yield c_summation(d1)
     self.assertEqual(a, 3)
     self.assertEqual(d1.result(), 1)
开发者ID:axisofentropy,项目名称:pulsar,代码行数:7,代码来源:coro.py


示例2: wait_fd

def wait_fd(fd, read=True):
    '''Wait for an event on file descriptor ``fd``.

    :param fd: file descriptor
    :param read=True: wait for a read event if ``True``, otherwise a wait
        for write event.

    This function must be invoked from a coroutine with parent, therefore
    invoking it from the main greenlet will raise an exception.
    Check how this function is used in the :func:`.psycopg2_wait_callback`
    function.
    '''
    current = greenlet.getcurrent()
    parent = current.parent
    assert parent, '"wait_fd" must be called by greenlet with a parent'
    try:
        fileno = fd.fileno()
    except AttributeError:
        fileno = fd
    loop = get_event_loop()
    future = Future(loop=loop)
    # When the event on fd occurs switch back to the current greenlet
    if read:
        loop.add_reader(fileno, _done_wait_fd, fileno, future, read)
    else:
        loop.add_writer(fileno, _done_wait_fd, fileno, future, read)
    # switch back to parent greenlet
    parent.switch(future)
    return future.result()
开发者ID:JinsongBian,项目名称:pulsar,代码行数:29,代码来源:__init__.py


示例3: test_call_at

 def test_call_at(self):
     loop = get_event_loop()
     d1 = Future()
     d2 = Future()
     c1 = loop.call_at(loop.time()+1, lambda: d1.set_result(loop.time()))
     c2 = loop.call_later(1, lambda: d2.set_result(loop.time()))
     t1, t2 = yield pulsar.multi_async((d1, d2))
     self.assertTrue(t1 <= t2)
开发者ID:axisofentropy,项目名称:pulsar,代码行数:8,代码来源:eventloop.py


示例4: test_json_with_async_string2

 def test_json_with_async_string2(self):
     d = Future()
     astr = wsgi.AsyncString(d)
     response = wsgi.Json({"bla": astr})
     self.assertEqual(len(response.children), 1)
     result = response.render()
     self.assertIsInstance(result, Future)
     d.set_result("ciao")
     result = yield from result
     self.assertEqual(result, json.dumps({"bla": "ciao"}))
开发者ID:dejlek,项目名称:pulsar,代码行数:10,代码来源:content.py


示例5: test_call_soon

 def test_call_soon(self):
     ioloop = get_event_loop()
     tid = yield loop_thread_id(ioloop)
     d = Future()
     callback = lambda: d.set_result(current_thread().ident)
     cbk = ioloop.call_soon(callback)
     self.assertEqual(cbk._callback, callback)
     self.assertEqual(cbk._args, ())
     # we should be able to wait less than a second
     result = yield d
     self.assertEqual(result, tid)
开发者ID:axisofentropy,项目名称:pulsar,代码行数:11,代码来源:eventloop.py


示例6: test_ping_pong_monitor

 def test_ping_pong_monitor(self):
     value = yield 3
     self.assertEqual(value, 3)
     try:
         future = Future()
         future.set_exception(ValueError('test'))
         yield future
     except ValueError:
         pass
     pong = yield send('monitor', 'ping')
     self.assertEqual(pong, 'pong')
开发者ID:axisofentropy,项目名称:pulsar,代码行数:11,代码来源:me.py


示例7: check_twisted

def check_twisted(deferred, loop):
    """Binding for twisted.

    Added to pulsar asynchronous engine via the :func:`.add_async_binding`
    function.
    """
    if isinstance(deferred, Deferred):
        future = Future(loop=loop)

        deferred.addCallbacks(future.set_result, lambda failure: future.set_exception(failure.value))

        return future
开发者ID:huobao36,项目名称:pulsar,代码行数:12,代码来源:__init__.py


示例8: _

 def _(*args, **kwargs):
     res = callable(*args, **kwargs)
     if isinstance(res, types.GeneratorType):
         res = _inlineCallbacks(None, res, Deferred())
     if isinstance(res, Deferred):
         future = Future()
         res.addCallbacks(
             future.set_result,
             lambda failure: future.set_exception(failure.value))
         future._deferred = res
         return future
     else:
         raise TypeError(
             "Callable %r should return a generator or a twisted Deferred"
             % callable)
开发者ID:axisofentropy,项目名称:pulsar,代码行数:15,代码来源:__init__.py


示例9: shutdown

 def shutdown(self, wait=True):
     with self._shutdown_lock:
         self._shutdown = True
         self._put(None)
         if wait:
             self._waiter = Future(loop=self._loop)
             return self._waiter
开发者ID:juanignaciocatalano,项目名称:pulsar,代码行数:7,代码来源:pool.py


示例10: test_multi

 def test_multi(self):
     d1 = Future()
     d2 = Future()
     d = multi_async([d1, d2, "bla"])
     self.assertFalse(d.done())
     d2.set_result("first")
     self.assertFalse(d.done())
     d1.set_result("second")
     result = yield from d
     self.assertEqual(result, ["second", "first", "bla"])
开发者ID:dejlek,项目名称:pulsar,代码行数:10,代码来源:multi.py


示例11: test_call_later

 def test_call_later(self):
     ioloop = get_event_loop()
     tid = yield loop_thread_id(ioloop)
     d = Future()
     timeout1 = ioloop.call_later(
         20, lambda: d.set_result(current_thread().ident))
     timeout2 = ioloop.call_later(
         10, lambda: d.set_result(current_thread().ident))
     # lets wake the ioloop
     self.assertTrue(has_callback(ioloop, timeout1))
     self.assertTrue(has_callback(ioloop, timeout2))
     timeout1.cancel()
     timeout2.cancel()
     self.assertTrue(timeout1._cancelled)
     self.assertTrue(timeout2._cancelled)
     timeout1 = ioloop.call_later(
         0.1, lambda: d.set_result(current_thread().ident))
     yield d
     self.assertTrue(d.done())
     self.assertEqual(d.result(), tid)
     self.assertFalse(has_callback(ioloop, timeout1))
开发者ID:JinsongBian,项目名称:pulsar,代码行数:21,代码来源:eventloop.py


示例12: GreenPool

class GreenPool(AsyncObject):
    """A pool of running greenlets.

    This pool maintains a group of greenlets to perform asynchronous
    tasks via the :meth:`submit` method.
    """
    worker_name = 'exec'

    def __init__(self, max_workers=None, loop=None):
        self._loop = loop or get_event_loop()
        self._max_workers = min(max_workers or _DEFAULT_WORKERS, _MAX_WORKERS)
        self._greenlets = set()
        self._available = set()
        self._queue = deque()
        self._shutdown = False
        self._waiter = None
        self._logger = logging.getLogger('pulsar.greenpool')
        self._shutdown_lock = threading.Lock()
        self.wait = wait

    @property
    def max_workers(self):
        return self._max_workers

    @max_workers.setter
    def max_workers(self, value):
        value = int(value)
        assert value > 0
        self._max_workers = value

    @property
    def in_green_worker(self):
        """True if the current greenlet is a green pool worker
        """
        return isinstance(getcurrent(), GreenletWorker)

    def submit(self, func, *args, **kwargs):
        """Equivalent to ``func(*args, **kwargs)``.

        This method create a new task for function ``func`` and adds it to
        the queue.
        Return a :class:`~asyncio.Future` called back once the task
        has finished.
        """
        with self._shutdown_lock:
            if self._shutdown:
                raise RuntimeError(
                    'cannot schedule new futures after shutdown')
            if self.in_green_worker:
                return wait(func(*args, **kwargs))
            else:
                future = Future(loop=self._loop)
                self._put((future, func, args, kwargs))
                return future

    def shutdown(self, wait=True):
        with self._shutdown_lock:
            self._shutdown = True
            self._put(None)
            if wait:
                self._waiter = Future(loop=self._loop)
                return self._waiter

    def getcurrent(self):
        return getcurrent()

    # INTERNALS
    def _adjust_greenlet_count(self):
        if (not self._shutdown and not self._available and
                len(self._greenlets) < self._max_workers):
            green = GreenletWorker(self._green_run)
            self._greenlets.add(green)
            self.logger.debug('Num greenlets: %d', len(self._greenlets))
            green.switch()
        return self._available

    def _put(self, task):
        # Run in the main greenlet of the evnet-loop thread
        self._queue.appendleft(task)
        self._check_queue()

    def _check_queue(self):
        # Run in the main greenlet of the event-loop thread
        if not self._adjust_greenlet_count():
            self.logger.debug('No greenlet available')
            return self._loop.call_soon(self._check_queue)
        try:
            task = self._queue.pop()
        except IndexError:
            return
        ensure_future(self._green_task(self._available.pop(), task),
                      loop=self._loop)

    async def _green_task(self, green, task):
        # Coroutine executing the in main greenlet
        # This coroutine is executed for every task put into the queue

        while task is not _DONE:
            # switch to the greenlet to start the task
            task = green.switch(task)
#.........这里部分代码省略.........
开发者ID:juanignaciocatalano,项目名称:pulsar,代码行数:101,代码来源:pool.py


示例13: GreenPool

class GreenPool(AsyncObject):
    '''A pool of running greenlets.

    This pool maintains a group of greenlets to perform asynchronous
    tasks via the :meth:`submit` method.
    '''
    worker_name = 'exec'

    def __init__(self, max_workers=None, loop=None, maxtasks=None):
        self._loop = loop or get_event_loop()
        self._max_workers = min(max_workers or _DEFAULT_WORKERS, _MAX_WORKERS)
        self._greenlets = set()
        self._available = set()
        self._maxtasks = maxtasks
        self._queue = deque()
        self._shutdown = False
        self._waiter = None
        self._shutdown_lock = threading.Lock()

    def submit(self, func, *args, **kwargs):
        '''Equivalent to ``func(*args, **kwargs)``.

        This method create a new task for function ``func`` and adds it to
        the queue.
        Return a :class:`~asyncio.Future` called back once the task
        has finished.
        '''
        with self._shutdown_lock:
            if self._shutdown:
                raise RuntimeError(
                    'cannot schedule new futures after shutdown')
            future = Future(loop=self._loop)
            self._put((future, func, args, kwargs))
            return future

    def shutdown(self, wait=True):
        with self._shutdown_lock:
            self._shutdown = True
            self._put()
            if wait:
                self._waiter = Future(loop=self._loop)
                return self._waiter

    # INTERNALS
    def _adjust_greenlet_count(self):
        if not self._available and len(self._greenlets) < self._max_workers:
            greenlet = GreenletWorker(self._green_run)
            self._greenlets.add(greenlet)
            greenlet.switch()

    def _put(self, task=None):
        # Run in the main greenlet of the evnet-loop thread
        if task:
            self._adjust_greenlet_count()
        self._queue.appendleft(task)
        self._check_queue()

    def _check_queue(self):
        # Run in the main greenlet of the event-loop thread
        if not self._available:
            return
        try:
            task = self._queue.pop()
        except IndexError:
            return
        async(self._green_task(self._available.pop(), task), loop=self._loop)

    def _green_task(self, greenlet, task):
        # Run in the main greenlet of the event-loop thread

        while task is not _DONE:
            # switch to the greenlet to start the task
            task = greenlet.switch(task)

            # if an asynchronous result is returned, yield from
            while is_async(task):
                try:
                    task = yield from task
                except Exception as exc:
                    # This call can return an asynchronous component
                    task = greenlet.throw(exc)

    def _green_run(self):
        # The run method of a worker greenlet
        task = True
        while task:
            greenlet = getcurrent()
            parent = greenlet.parent
            assert parent
            self._available.add(greenlet)
            self._loop.call_soon(self._check_queue)
            task = parent.switch(_DONE)  # switch back to the main execution
            if task:
                # If a new task is available execute it
                # Here we are in the child greenlet
                future, func, args, kwargs = task
                try:
                    result = func(*args, **kwargs)
                except Exception as exc:
                    future.set_exception(exc)
#.........这里部分代码省略.........
开发者ID:LJS109,项目名称:pulsar,代码行数:101,代码来源:__init__.py


示例14: __init__

 def __init__(self, headers, parser, transport=None):
     self.headers = headers
     self.parser = parser
     self.transport = transport
     self.buffer = b''
     self.on_message_complete = Future()
开发者ID:Ghost-script,项目名称:dyno-chat,代码行数:6,代码来源:server.py



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python addon.ADDON类代码示例发布时间:2022-05-25
下一篇:
Python pulsar.Config类代码示例发布时间:2022-05-25
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap