當前位置: 首頁>>代碼示例>>Python>>正文


Python asyncio.TimerHandle方法代碼示例

本文整理匯總了Python中asyncio.TimerHandle方法的典型用法代碼示例。如果您正苦於以下問題:Python asyncio.TimerHandle方法的具體用法?Python asyncio.TimerHandle怎麽用?Python asyncio.TimerHandle使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在asyncio的用法示例。


在下文中一共展示了asyncio.TimerHandle方法的13個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: run_in_executor

# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import TimerHandle [as 別名]
def run_in_executor(self, executor, callback, *args):
        """Run callback in executor.

        If no executor is provided, the default executor will be used, which defers execution to
        a background thread.
        """
        self._logger.debug('Running callback {} with args {} in executor'.format(callback, args))
        if isinstance(callback, asyncio.Handle):
            assert not args
            assert not isinstance(callback, asyncio.TimerHandle)
            if callback._cancelled:
                f = asyncio.Future()
                f.set_result(None)
                return f
            callback, args = callback.callback, callback.args

        if executor is None:
            self._logger.debug('Using default executor')
            executor = self.__default_executor

        if executor is None:
            self._logger.debug('Creating default executor')
            executor = self.__default_executor = QThreadExecutor()

        return asyncio.wrap_future(executor.submit(callback, *args)) 
開發者ID:gmarull,項目名稱:asyncqt,代碼行數:27,代碼來源:__init__.py

示例2: test_timer

# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import TimerHandle [as 別名]
def test_timer(self):
        def callback(*args):
            return args

        args = (1, 2, 3)
        when = time.monotonic()
        h = asyncio.TimerHandle(when, callback, args, mock.Mock())
        self.assertIs(h._callback, callback)
        self.assertIs(h._args, args)
        self.assertFalse(h._cancelled)

        # cancel
        h.cancel()
        self.assertTrue(h._cancelled)
        self.assertIsNone(h._callback)
        self.assertIsNone(h._args)

        # when cannot be None
        self.assertRaises(AssertionError,
                          asyncio.TimerHandle, None, callback, args,
                          self.loop) 
開發者ID:Microvellum,項目名稱:Fluid-Designer,代碼行數:23,代碼來源:test_events.py

示例3: test_timer_repr_debug

# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import TimerHandle [as 別名]
def test_timer_repr_debug(self):
        self.loop.get_debug.return_value = True

        # simple function
        create_filename = __file__
        create_lineno = sys._getframe().f_lineno + 1
        h = asyncio.TimerHandle(123, noop, (), self.loop)
        filename, lineno = test_utils.get_function_source(noop)
        self.assertEqual(repr(h),
                        '<TimerHandle when=123 noop() '
                        'at %s:%s created at %s:%s>'
                        % (filename, lineno, create_filename, create_lineno))

        # cancelled handle
        h.cancel()
        self.assertEqual(repr(h),
                        '<TimerHandle cancelled when=123 noop() '
                        'at %s:%s created at %s:%s>'
                        % (filename, lineno, create_filename, create_lineno)) 
開發者ID:Microvellum,項目名稱:Fluid-Designer,代碼行數:21,代碼來源:test_events.py

示例4: test__run_once

# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import TimerHandle [as 別名]
def test__run_once(self):
        h1 = asyncio.TimerHandle(time.monotonic() + 5.0, lambda: True, (),
                                 self.loop)
        h2 = asyncio.TimerHandle(time.monotonic() + 10.0, lambda: True, (),
                                 self.loop)

        h1.cancel()

        self.loop._process_events = mock.Mock()
        self.loop._scheduled.append(h1)
        self.loop._scheduled.append(h2)
        self.loop._run_once()

        t = self.loop._selector.select.call_args[0][0]
        self.assertTrue(9.5 < t < 10.5, t)
        self.assertEqual([h2], self.loop._scheduled)
        self.assertTrue(self.loop._process_events.called) 
開發者ID:Microvellum,項目名稱:Fluid-Designer,代碼行數:19,代碼來源:test_base_events.py

示例5: test__run_once_schedule_handle

# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import TimerHandle [as 別名]
def test__run_once_schedule_handle(self):
        handle = None
        processed = False

        def cb(loop):
            nonlocal processed, handle
            processed = True
            handle = loop.call_soon(lambda: True)

        h = asyncio.TimerHandle(time.monotonic() - 1, cb, (self.loop,),
                                self.loop)

        self.loop._process_events = mock.Mock()
        self.loop._scheduled.append(h)
        self.loop._run_once()

        self.assertTrue(processed)
        self.assertEqual([handle], list(self.loop._ready)) 
開發者ID:Microvellum,項目名稱:Fluid-Designer,代碼行數:20,代碼來源:test_base_events.py

示例6: __init__

# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import TimerHandle [as 別名]
def __init__(
        self,
        request: Message,
        addr: Tuple[str, int],
        protocol,
        retransmissions: Optional[int] = None,
    ) -> None:
        self.__addr = addr
        self.__future: asyncio.Future[
            Tuple[Message, Tuple[str, int]]
        ] = asyncio.Future()
        self.__request = request
        self.__timeout_delay = RETRY_RTO
        self.__timeout_handle: Optional[asyncio.TimerHandle] = None
        self.__protocol = protocol
        self.__tries = 0
        self.__tries_max = 1 + (
            retransmissions if retransmissions is not None else RETRY_MAX
        ) 
開發者ID:aiortc,項目名稱:aioice,代碼行數:21,代碼來源:stun.py

示例7: __init__

# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import TimerHandle [as 別名]
def __init__(self,
                 loop: asyncio.AbstractEventLoop) -> None:
        BaseProtocol.__init__(self, loop=loop)
        DataQueue.__init__(self, loop)

        self._should_close = False

        self._payload = None
        self._skip_payload = False
        self._payload_parser = None

        self._timer = None

        self._tail = b''
        self._upgraded = False
        self._parser = None  # type: Optional[HttpResponseParser]

        self._read_timeout = None  # type: Optional[float]
        self._read_timeout_handle = None  # type: Optional[asyncio.TimerHandle] 
開發者ID:TouwaStar,項目名稱:Galaxy_Plugin_Bethesda,代碼行數:21,代碼來源:client_proto.py

示例8: __init__

# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import TimerHandle [as 別名]
def __init__(self, servers, timeout, retry_policy, allow_read_only, read_timeout, loop):
        self.loop = loop
        self.hosts = []
        for server in servers.split(","):
            ipv6_match = re.match(r'\[(.*)\]:(\d+)$', server)
            if ipv6_match is not None:
                host, port = ipv6_match.groups()
            elif ":" in server:
                host, port = server.rsplit(":", 1)
            else:
                host = server
                port = DEFAULT_ZOOKEEPER_PORT

            self.hosts.append((host, port))

        self.conn = None
        self.state = SessionStateMachine(session=self)

        self.retry_policy = retry_policy or RetryPolicy.forever()
        self.allow_read_only = allow_read_only

        self.xid = 0
        self.last_zxid = None

        self.session_id = None
        self.timeout = timeout
        self.password = b'\x00'
        self.read_timeout = read_timeout

        self.repair_loop_task = None

        # asyncio.TimerHandle object
        self.heartbeat_handle = None
        # asyncio.Task object
        self.heartbeat_task = None

        self.watch_callbacks = collections.defaultdict(set)

        self.started = False
        self.closing = False 
開發者ID:micro-fan,項目名稱:aiozk,代碼行數:42,代碼來源:session.py

示例9: test_hash

# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import TimerHandle [as 別名]
def test_hash(self):
        when = time.monotonic()
        h = asyncio.TimerHandle(when, lambda: False, (),
                                mock.Mock())
        self.assertEqual(hash(h), hash(when)) 
開發者ID:Microvellum,項目名稱:Fluid-Designer,代碼行數:7,代碼來源:test_events.py

示例10: test_timer_repr

# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import TimerHandle [as 別名]
def test_timer_repr(self):
        self.loop.get_debug.return_value = False

        # simple function
        h = asyncio.TimerHandle(123, noop, (), self.loop)
        src = test_utils.get_function_source(noop)
        self.assertEqual(repr(h),
                        '<TimerHandle when=123 noop() at %s:%s>' % src)

        # cancelled handle
        h.cancel()
        self.assertEqual(repr(h),
                        '<TimerHandle cancelled when=123>') 
開發者ID:Microvellum,項目名稱:Fluid-Designer,代碼行數:15,代碼來源:test_events.py

示例11: test_timer_comparison

# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import TimerHandle [as 別名]
def test_timer_comparison(self):
        def callback(*args):
            return args

        when = time.monotonic()

        h1 = asyncio.TimerHandle(when, callback, (), self.loop)
        h2 = asyncio.TimerHandle(when, callback, (), self.loop)
        # TODO: Use assertLess etc.
        self.assertFalse(h1 < h2)
        self.assertFalse(h2 < h1)
        self.assertTrue(h1 <= h2)
        self.assertTrue(h2 <= h1)
        self.assertFalse(h1 > h2)
        self.assertFalse(h2 > h1)
        self.assertTrue(h1 >= h2)
        self.assertTrue(h2 >= h1)
        self.assertTrue(h1 == h2)
        self.assertFalse(h1 != h2)

        h2.cancel()
        self.assertFalse(h1 == h2)

        h1 = asyncio.TimerHandle(when, callback, (), self.loop)
        h2 = asyncio.TimerHandle(when + 10.0, callback, (), self.loop)
        self.assertTrue(h1 < h2)
        self.assertFalse(h2 < h1)
        self.assertTrue(h1 <= h2)
        self.assertFalse(h2 <= h1)
        self.assertFalse(h1 > h2)
        self.assertTrue(h2 > h1)
        self.assertFalse(h1 >= h2)
        self.assertTrue(h2 >= h1)
        self.assertFalse(h1 == h2)
        self.assertTrue(h1 != h2)

        h3 = asyncio.Handle(callback, (), self.loop)
        self.assertIs(NotImplemented, h1.__eq__(h3))
        self.assertIs(NotImplemented, h1.__ne__(h3)) 
開發者ID:Microvellum,項目名稱:Fluid-Designer,代碼行數:41,代碼來源:test_events.py

示例12: test_run_once_in_executor_handle

# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import TimerHandle [as 別名]
def test_run_once_in_executor_handle(self):
        def cb():
            pass

        self.assertRaises(
            AssertionError, self.loop.run_in_executor,
            None, asyncio.Handle(cb, (), self.loop), ('',))
        self.assertRaises(
            AssertionError, self.loop.run_in_executor,
            None, asyncio.TimerHandle(10, cb, (), self.loop)) 
開發者ID:Microvellum,項目名稱:Fluid-Designer,代碼行數:12,代碼來源:test_base_events.py

示例13: test_default_exc_handler_callback

# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import TimerHandle [as 別名]
def test_default_exc_handler_callback(self):
        self.loop._process_events = mock.Mock()

        def zero_error(fut):
            fut.set_result(True)
            1/0

        # Test call_soon (events.Handle)
        with mock.patch('asyncio.base_events.logger') as log:
            fut = asyncio.Future(loop=self.loop)
            self.loop.call_soon(zero_error, fut)
            fut.add_done_callback(lambda fut: self.loop.stop())
            self.loop.run_forever()
            log.error.assert_called_with(
                test_utils.MockPattern('Exception in callback.*zero'),
                exc_info=(ZeroDivisionError, MOCK_ANY, MOCK_ANY))

        # Test call_later (events.TimerHandle)
        with mock.patch('asyncio.base_events.logger') as log:
            fut = asyncio.Future(loop=self.loop)
            self.loop.call_later(0.01, zero_error, fut)
            fut.add_done_callback(lambda fut: self.loop.stop())
            self.loop.run_forever()
            log.error.assert_called_with(
                test_utils.MockPattern('Exception in callback.*zero'),
                exc_info=(ZeroDivisionError, MOCK_ANY, MOCK_ANY)) 
開發者ID:Microvellum,項目名稱:Fluid-Designer,代碼行數:28,代碼來源:test_base_events.py


注:本文中的asyncio.TimerHandle方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。