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