本文整理汇总了Python中asyncio.futures.Future方法的典型用法代码示例。如果您正苦于以下问题:Python futures.Future方法的具体用法?Python futures.Future怎么用?Python futures.Future使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类asyncio.futures
的用法示例。
在下文中一共展示了futures.Future方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: wait
# 需要导入模块: from asyncio import futures [as 别名]
# 或者: from asyncio.futures import Future [as 别名]
def wait(self, state_or_states: Union[Any, Sequence[Any]]) -> Tuple[Any, Any]:
"""Wait state to be set.
:params state_or_states: state or list of states.
:return: tuple of previous state and new state.
"""
states = ensure_list(state_or_states)
if self._state in states:
return (None, self._state)
watcher: Future = Future()
watcher._states = states # type: ignore # pylint: disable=protected-access
self._watchers.add(watcher)
try:
return await watcher
finally:
self._remove_watcher(watcher)
示例2: _copy_future_state
# 需要导入模块: from asyncio import futures [as 别名]
# 或者: from asyncio.futures import Future [as 别名]
def _copy_future_state(source, dest):
"""Internal helper to copy state from another Future.
The other Future may be a concurrent.futures.Future.
"""
assert source.done()
if dest.cancelled():
return
assert not dest.done()
if source.cancelled():
dest.cancel()
else:
exception = source.exception()
if exception is not None:
dest.set_exception(exception)
else:
result = source.result()
dest.set_result(result)
示例3: run_callback_threadsafe
# 需要导入模块: from asyncio import futures [as 别名]
# 或者: from asyncio.futures import Future [as 别名]
def run_callback_threadsafe(loop, callback, *args):
"""Submit a callback object to a given event loop.
Return a concurrent.futures.Future to access the result.
"""
ident = loop.__dict__.get("_thread_ident")
if ident is not None and ident == threading.get_ident():
raise RuntimeError('Cannot be called from within the event loop')
future = concurrent.futures.Future()
def run_callback():
"""Run callback and store result."""
try:
future.set_result(callback(*args))
# pylint: disable=broad-except
except Exception as exc:
if future.set_running_or_notify_cancel():
future.set_exception(exc)
else:
_LOGGER.warning("Exception on lost future: ", exc_info=True)
loop.call_soon_threadsafe(run_callback)
return future
示例4: mqtt_subscribe
# 需要导入模块: from asyncio import futures [as 别名]
# 或者: from asyncio.futures import Future [as 别名]
def mqtt_subscribe(self, topics, packet_id):
"""
:param topics: array of topics [{'filter':'/a/b', 'qos': 0x00}, ...]
:return:
"""
# Build and send SUBSCRIBE message
subscribe = SubscribePacket.build(topics, packet_id)
yield from self._send_packet(subscribe)
# Wait for SUBACK is received
waiter = futures.Future(loop=self._loop)
self._subscriptions_waiter[subscribe.variable_header.packet_id] = waiter
return_codes = yield from waiter
del self._subscriptions_waiter[subscribe.variable_header.packet_id]
return return_codes
示例5: request_future
# 需要导入模块: from asyncio import futures [as 别名]
# 或者: from asyncio.futures import Future [as 别名]
def request_future(self, target: PID, message: object, timeout: timedelta = None,
cancellation_token: CancelToken = None) -> asyncio.Future:
return await self._context.request_future(target, message, timeout, cancellation_token)
示例6: stop_future
# 需要导入模块: from asyncio import futures [as 别名]
# 或者: from asyncio.futures import Future [as 别名]
def stop_future(self, pid: PID) -> asyncio.Future:
return await self._context.stop_future(pid)
示例7: poison_future
# 需要导入模块: from asyncio import futures [as 别名]
# 或者: from asyncio.futures import Future [as 别名]
def poison_future(self, pid: PID) -> asyncio.Future:
return await self._context.poison_future(pid)
示例8: predict
# 需要导入模块: from asyncio import futures [as 别名]
# 或者: from asyncio.futures import Future [as 别名]
def predict(self, x):
future = Future()
self.tasks.append((x, future))
if (len(self.tasks) >= self.bulk_size):
self._flush()
return future
示例9: __init__
# 需要导入模块: from asyncio import futures [as 别名]
# 或者: from asyncio.futures import Future [as 别名]
def __init__(self, initial_state: Any = None):
"""Init async state.
:param initial_state: state to set on start.
"""
self._state = initial_state
self._watchers: Set[Future] = set()
示例10: _remove_watcher
# 需要导入模块: from asyncio import futures [as 别名]
# 或者: from asyncio.futures import Future [as 别名]
def _remove_watcher(self, watcher: Future) -> None:
"""Remove watcher for state wait."""
try:
self._watchers.remove(watcher)
except KeyError:
pass
示例11: _watcher_result_callback
# 需要导入模块: from asyncio import futures [as 别名]
# 或者: from asyncio.futures import Future [as 别名]
def _watcher_result_callback(self, watcher: Future) -> Callable:
"""Create callback for watcher result."""
# docstyle.
def _callback(result):
if watcher.done():
return
watcher.set_result(result)
return _callback
示例12: _set_concurrent_future_state
# 需要导入模块: from asyncio import futures [as 别名]
# 或者: from asyncio.futures import Future [as 别名]
def _set_concurrent_future_state(concurr, source):
"""Copy state from a future to a concurrent.futures.Future."""
assert source.done()
if source.cancelled():
concurr.cancel()
if not concurr.set_running_or_notify_cancel():
return
exception = source.exception()
if exception is not None:
concurr.set_exception(exception)
else:
result = source.result()
concurr.set_result(result)
示例13: _chain_future
# 需要导入模块: from asyncio import futures [as 别名]
# 或者: from asyncio.futures import Future [as 别名]
def _chain_future(source, destination):
"""Chain two futures so that when one completes, so does the other.
The result (or exception) of source will be copied to destination.
If destination is cancelled, source gets cancelled too.
Compatible with both asyncio.Future and concurrent.futures.Future.
"""
if not isinstance(source, (Future, concurrent.futures.Future)):
raise TypeError('A future is required for source argument')
if not isinstance(destination, (Future, concurrent.futures.Future)):
raise TypeError('A future is required for destination argument')
# pylint: disable=protected-access
source_loop = source._loop if isinstance(source, Future) else None
dest_loop = destination._loop if isinstance(destination, Future) else None
def _set_state(future, other):
if isinstance(future, Future):
_copy_future_state(other, future)
else:
_set_concurrent_future_state(future, other)
def _call_check_cancel(destination):
if destination.cancelled():
if source_loop is None or source_loop is dest_loop:
source.cancel()
else:
source_loop.call_soon_threadsafe(source.cancel)
def _call_set_state(source):
if dest_loop is None or dest_loop is source_loop:
_set_state(destination, source)
else:
dest_loop.call_soon_threadsafe(_set_state, destination, source)
destination.add_done_callback(_call_check_cancel)
source.add_done_callback(_call_set_state)
示例14: run_coroutine_threadsafe
# 需要导入模块: from asyncio import futures [as 别名]
# 或者: from asyncio.futures import Future [as 别名]
def run_coroutine_threadsafe(coro, loop):
"""Submit a coroutine object to a given event loop.
Return a concurrent.futures.Future to access the result.
"""
ident = loop.__dict__.get("_thread_ident")
if ident is not None and ident == threading.get_ident():
raise RuntimeError('Cannot be called from within the event loop')
if not coroutines.iscoroutine(coro):
raise TypeError('A coroutine object is required')
future = concurrent.futures.Future()
def callback():
"""Callback to call the coroutine."""
try:
# pylint: disable=deprecated-method
_chain_future(ensure_future(coro, loop=loop), future)
# pylint: disable=broad-except
except Exception as exc:
if future.set_running_or_notify_cancel():
future.set_exception(exc)
else:
_LOGGER.warning("Exception on lost future: ", exc_info=True)
loop.call_soon_threadsafe(callback)
return future
示例15: start
# 需要导入模块: from asyncio import futures [as 别名]
# 或者: from asyncio.futures import Future [as 别名]
def start(self):
yield from super().start()
if self._disconnect_waiter is None:
self._disconnect_waiter = futures.Future(loop=self._loop)