本文整理匯總了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))
示例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)
示例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))
示例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)
示例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))
示例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
)
示例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]
示例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
示例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))
示例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>')
示例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))
示例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))
示例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))