本文整理汇总了Python中asyncio.shield方法的典型用法代码示例。如果您正苦于以下问题:Python asyncio.shield方法的具体用法?Python asyncio.shield怎么用?Python asyncio.shield使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类asyncio
的用法示例。
在下文中一共展示了asyncio.shield方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: downstream
# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import shield [as 别名]
def downstream(self):
try:
self.upstream_buffer_task = \
asyncio.ensure_future(self.consume_upstream_buffer())
async for msg in self.up_conn:
if msg.type == aiohttp.WSMsgType.TEXT:
await self.down_conn.send_str(msg.data)
if self.downstream_cb is not None:
await asyncio.shield(self.downstream_cb(msg.data))
if msg.type == aiohttp.WSMsgType.BINARY:
await self.down_conn.send_bytes(msg.data)
if self.downstream_cb is not None:
await asyncio.shield(self.downstream_cb(msg.data))
elif msg.type == aiohttp.WSMsgType.CLOSED:
break
elif msg.type == aiohttp.WSMsgType.ERROR:
break
# here, server gracefully disconnected
except asyncio.CancelledError:
raise
except Exception:
log.exception('unexpected error')
finally:
await self.close_upstream()
示例2: _redirect_async
# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import shield [as 别名]
def _redirect_async(self, redirect, auth):
"""Redirect the client endpoint using a Link DETACH redirect
response.
:param redirect: The Link DETACH redirect details.
:type redirect: ~uamqp.errors.LinkRedirect
:param auth: Authentication credentials to the redirected endpoint.
:type auth: ~uamqp.authentication.common.AMQPAuth
"""
# pylint: disable=protected-access
if not self._connection._cbs:
_logger.info("Closing non-CBS session.")
await asyncio.shield(self._session.destroy_async(), loop=self.loop)
self._session = None
self._auth = auth
self._hostname = self._remote_address.hostname
await self._connection.redirect_async(redirect, auth)
await self._build_session_async()
示例3: _client_run_async
# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import shield [as 别名]
def _client_run_async(self):
"""MessageSender Link is now open - perform message send
on all pending messages.
Will return True if operation successful and client can remain open for
further work.
:rtype: bool
"""
# pylint: disable=protected-access
await self.message_handler.work_async()
self._waiting_messages = 0
async with self._pending_messages_lock:
self._pending_messages = await self._filter_pending_async()
if self._backoff and not self._waiting_messages:
_logger.info("Client told to backoff - sleeping for %r seconds", self._backoff)
await self._connection.sleep_async(self._backoff)
self._backoff = 0
await asyncio.shield(self._connection.work_async(), loop=self.loop)
return True
示例4: send_offsets_to_transaction
# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import shield [as 别名]
def send_offsets_to_transaction(self, offsets, group_id):
self._ensure_transactional()
if not self._txn_manager.is_in_transaction():
raise IllegalOperation("Not in the middle of a transaction")
if not group_id or not isinstance(group_id, str):
raise ValueError(group_id)
# validate `offsets` structure
formatted_offsets = commit_structure_validate(offsets)
log.debug(
"Begin adding offsets %s for consumer group %s to transaction",
formatted_offsets, group_id)
fut = self._txn_manager.add_offsets_to_txn(formatted_offsets, group_id)
await asyncio.shield(fut, loop=self._loop)
示例5: _listener
# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import shield [as 别名]
def _listener(self, subscription, interval, callback):
while True:
try:
# FIXME: This should use an async requester as below
packet = callback()
self.__handle_packet(packet, subscription)
await asyncio.sleep(interval)
except asyncio.CancelledError:
## unsubscribe from all subscriptions
confirmations = await asyncio.gather(
asyncio.shield(self._unsubscribe(subscription))
for subscription in self.subscriptions)
except Exception as ex:
logger.error(ex)
logger.error(packet)
raise
示例6: disconnect
# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import shield [as 别名]
def disconnect(self, timeout: Optional[float] = None) -> None:
"""Close the connection gracefully."""
await self._connection.close(code=1000)
try:
await asyncio.wait_for(
asyncio.shield(self._message_processor), timeout or self.TIMEOUT
)
self._message_processor.result()
except asyncio.TimeoutError:
pass
finally:
if not self._message_processor.done():
self._message_processor.cancel()
try:
await self._message_processor
except asyncio.CancelledError:
pass
示例7: init_workers
# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import shield [as 别名]
def init_workers(app: web.Application, conf: WorkersConfig) -> ThreadPoolExecutor:
n = conf.max_workers
executor = ThreadPoolExecutor(max_workers=n)
loop = asyncio.get_event_loop()
run = loop.run_in_executor
fs = [run(executor, warm, conf.path_to_model_state) for _ in range(0, n)]
await asyncio.gather(*fs)
async def close_executor(app: web.Application) -> None:
fs = [run(executor, clean) for _ in range(0, n)]
await asyncio.shield(asyncio.gather(*fs))
executor.shutdown(wait=True)
app.on_cleanup.append(close_executor)
app["executor"] = executor
return executor
示例8: test_shield_effect
# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import shield [as 别名]
def test_shield_effect(self):
# Cancelling outer() does not affect inner().
proof = 0
waiter = asyncio.Future(loop=self.loop)
@asyncio.coroutine
def inner():
nonlocal proof
yield from waiter
proof += 1
@asyncio.coroutine
def outer():
nonlocal proof
yield from asyncio.shield(inner(), loop=self.loop)
proof += 100
f = asyncio.ensure_future(outer(), loop=self.loop)
test_utils.run_briefly(self.loop)
f.cancel()
with self.assertRaises(asyncio.CancelledError):
self.loop.run_until_complete(f)
waiter.set_result(None)
test_utils.run_briefly(self.loop)
self.assertEqual(proof, 1)
示例9: test_gather_shield
# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import shield [as 别名]
def test_gather_shield(self):
child1 = asyncio.Future(loop=self.loop)
child2 = asyncio.Future(loop=self.loop)
inner1 = asyncio.shield(child1, loop=self.loop)
inner2 = asyncio.shield(child2, loop=self.loop)
parent = asyncio.gather(inner1, inner2, loop=self.loop)
test_utils.run_briefly(self.loop)
parent.cancel()
# This should cancel inner1 and inner2 but bot child1 and child2.
test_utils.run_briefly(self.loop)
self.assertIsInstance(parent.exception(), asyncio.CancelledError)
self.assertTrue(inner1.cancelled())
self.assertTrue(inner2.cancelled())
child1.set_result(1)
child2.set_result(2)
test_utils.run_briefly(self.loop)
示例10: test_maxsize_release
# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import shield [as 别名]
def test_maxsize_release(self):
pool = Pool(url="ws://localhost:8182/",
maxsize=2,
username="stephen",
password="password",
future_class=Future)
async def go():
c1 = await pool.acquire()
c2 = await pool.acquire()
c3 = pool.acquire()
self.assertIsInstance(c3, Future)
with self.assertRaises(asyncio.TimeoutError):
shielded_fut = asyncio.shield(c3)
await asyncio.wait_for(shielded_fut, 0.1)
await pool.release(c2)
c3 = await c3
self.assertEqual(c2, c3)
c1.conn.close()
c2.conn.close()
c3.conn.close()
self.loop.run_until_complete(go())
示例11: test_maxsize_release
# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import shield [as 别名]
def test_maxsize_release(self):
pool = Pool(url="ws://localhost:8182/",
maxsize=2,
username="stephen",
password="password",
future_class=Future)
@asyncio.coroutine
def go():
c1 = yield from pool.acquire()
c2 = yield from pool.acquire()
c3 = pool.acquire()
self.assertIsInstance(c3, Future)
with self.assertRaises(asyncio.TimeoutError):
shielded_fut = asyncio.shield(c3)
yield from asyncio.wait_for(shielded_fut, 0.1)
yield from pool.release(c2)
c3 = yield from c3
self.assertEqual(c2, c3)
c1.conn.close()
c2.conn.close()
c3.conn.close()
self.loop.run_until_complete(go())
示例12: get_tx_events
# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import shield [as 别名]
def get_tx_events(self):
org = 'org1.example.com'
peer = self.client.get_peer('peer0.' + org)
org_admin = self.client.get_user(org, 'Admin')
channel = self.client.get_channel(self.channel_name)
channel_event_hub = channel.newChannelEventHub(peer, org_admin)
stream = channel_event_hub.connect(start='oldest',
stop='newest', filtered=False)
self.txs = {}
channel_event_hub.registerTxEvent('all', onEvent=self.onTxEvent)
try:
await shield(stream)
except Exception:
pass
channel_event_hub.disconnect()
self.assertEqual(len(self.txs['all']), 4)
示例13: _fail
# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import shield [as 别名]
def _fail(self, e: Exception, clean_close: bool = True) -> None:
if self.close_task is None:
self.close_task = asyncio.shield(
asyncio.ensure_future(self._close_coro(e, clean_close=clean_close))
)
示例14: disconnected
# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import shield [as 别名]
def disconnected(self):
"""
Future that resolves when the connection to Telegram
ends, either by user action or in the background.
Note that it may resolve in either a ``ConnectionError``
or any other unexpected error that could not be handled.
"""
return asyncio.shield(self._disconnected, loop=self._loop)
# Private methods
示例15: _scheduled_task
# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import shield [as 别名]
def _scheduled_task(self, data: TaskData) -> None:
"""Await the `data.callback` coroutine after waiting for `data.wait_time` seconds."""
try:
log.trace(f"Waiting {data.wait_time} seconds before awaiting callback.")
await asyncio.sleep(data.wait_time)
# Use asyncio.shield to prevent callback from cancelling itself.
# The parent task (_scheduled_task) will still get cancelled.
log.trace("Done waiting; now awaiting the callback.")
await asyncio.shield(data.callback)
finally:
if inspect.iscoroutine(data.callback):
log.trace("Explicitly closing coroutine.")
data.callback.close()