Python源码示例:asyncio.CancelledError()
示例1
def patcher(self):
await self.bot.wait_until_ready()
try:
await asyncio.sleep(6) # be safe lolz
while True:
if not hasattr(self.bot.send_message, 'old'):
print(
'[WARNING:] -- Overwriting bot.send_message with '
'send_lolz. If bot.send_message is not reloaded,')
print(
'[WARNING:] -- in the event of a crash of the lolz '
'cog, you may not be able revert to bot.send_message '
'without a restart/reloading lolz')
self.bot.send_message = self.send_lolz(self.bot.send_message)
await asyncio.sleep(1)
except asyncio.CancelledError:
pass
示例2
def sse():
queue = asyncio.Queue()
app.clients.add(queue)
async def send_events():
while True:
try:
data = await queue.get()
event = ServerSentEvent(data)
yield event.encode()
except asyncio.CancelledError as error:
app.clients.remove(queue)
response = await make_response(
send_events(),
{
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
'Transfer-Encoding': 'chunked',
},
)
response.timeout = None
return response
示例3
def test_app_handle_request_asyncio_cancelled_error() -> None:
app = Quart(__name__)
@app.route("/")
async def index() -> NoReturn:
raise asyncio.CancelledError()
request = app.request_class(
"GET",
"http",
"/",
b"",
Headers([("host", "quart.com")]),
"",
"1.1",
send_push_promise=no_op_push,
)
with pytest.raises(asyncio.CancelledError):
await app.handle_request(request)
示例4
def test_middleware_response_raise_cancelled_error(app, caplog):
app.config.RESPONSE_TIMEOUT = 1
@app.middleware("response")
async def process_response(request, response):
raise CancelledError("CancelledError at response middleware")
@app.get("/")
def handler(request):
return text("OK")
with caplog.at_level(logging.ERROR):
reqrequest, response = app.test_client.get("/")
assert response.status == 503
assert (
"sanic.root",
logging.ERROR,
"Exception occurred while handling uri: 'http://127.0.0.1:42101/'",
) not in caplog.record_tuples
示例5
def start(self, handler):
print(f"-- Listening for rabbitmq messages on queue {self.queue} --")
self._handler = handler
await self._channel_ready.wait()
# channel hasn't actually been bootstraped yet
await self._bootstrap_channel(self.channel)
try:
await self._done_future
except asyncio.CancelledError:
pass
# shutting down
logger.warning("Shutting down rabbitmq transport")
await self.channel.basic_cancel(self._consumer_tag)
await self.close()
while self._counter > 0:
await asyncio.sleep(1)
示例6
def _send_loop(self):
"""
This loop is constantly popping items off the queue to send them.
"""
try:
while self._connected:
self._send(await self._send_queue.get())
await self._writer.drain()
except asyncio.CancelledError:
pass
except Exception as e:
if isinstance(e, IOError):
self._log.info('The server closed the connection while sending')
else:
self._log.exception('Unexpected exception in the send loop')
await self.disconnect()
示例7
def test_quit(redis):
expected = (ConnectionClosedError, ConnectionError)
try:
assert b'OK' == await redis.quit()
except expected:
pass
if not isinstance(redis.connection, ConnectionsPool):
# reader task may not yet been cancelled and _do_close not called
# so the ConnectionClosedError may be raised (or ConnectionError)
with pytest.raises(expected):
try:
await redis.ping()
except asyncio.CancelledError:
assert False, "Cancelled error must not be raised"
# wait one loop iteration until it get surely closed
await asyncio.sleep(0)
assert redis.connection.closed
with pytest.raises(ConnectionClosedError):
await redis.ping()
示例8
def view_logs(server: str, token: str) -> None:
async with ClientSession() as session:
async with session.ws_connect(f"{server}/_matrix/maubot/v1/logs") as ws:
await ws.send_str(token)
try:
msg: WSMessage
async for msg in ws:
if msg.type == WSMsgType.TEXT:
if not handle_msg(msg.json()):
break
elif msg.type == WSMsgType.ERROR:
print(Fore.YELLOW + "Connection error: " + msg.data + Fore.RESET)
elif msg.type == WSMsgType.CLOSE:
print(Fore.YELLOW + "Server closed connection" + Fore.RESET)
except asyncio.CancelledError:
pass
示例9
def run_async_coroutine(self, coroutine_to_run, timeout):
"""Start coroutine in dedicated thread and await its result with timeout"""
start_time = time.time()
coro_future = self.start_async_coroutine(coroutine_to_run)
# run_coroutine_threadsafe returns future as concurrent.futures.Future() and not asyncio.Future
# so, we can await it with timeout inside current thread
try:
coro_result = coro_future.result(timeout=timeout)
self.logger.debug("scheduled {} returned {}".format(coroutine_to_run, coro_result))
return coro_result
except concurrent.futures.TimeoutError:
passed = time.time() - start_time
raise MolerTimeout(timeout=timeout,
kind="run_async_coroutine({})".format(coroutine_to_run),
passed_time=passed)
except concurrent.futures.CancelledError:
raise
示例10
def simple_db_mutate_returning_item(result_cls, context, mutation_query, *,
item_query, item_cls):
async with context['dbpool'].acquire() as conn, conn.begin():
try:
result = await conn.execute(mutation_query)
if result.rowcount > 0:
result = await conn.execute(item_query)
item = await result.first()
return result_cls(True, 'success', item_cls.from_row(item))
else:
return result_cls(False, 'no matching record', None)
except (pg.IntegrityError, sa.exc.IntegrityError) as e:
return result_cls(False, f'integrity error: {e}', None)
except (asyncio.CancelledError, asyncio.TimeoutError):
raise
except Exception as e:
return result_cls(False, f'unexpected error: {e}', None)
示例11
def catch_unexpected(log, reraise_cancellation: bool = True, raven=None):
def _wrap(func):
@functools.wraps(func)
async def _wrapped(*args, **kwargs):
try:
return await func(*args, **kwargs)
except asyncio.CancelledError:
if reraise_cancellation:
raise
except Exception:
if raven:
raven.captureException()
log.exception('unexpected error!')
raise
return _wrapped
return _wrap
示例12
def check_agent_lost(app, interval):
try:
now = datetime.now(tzutc())
timeout = timedelta(seconds=app['config']['manager']['heartbeat-timeout'])
async def _check_impl():
async for agent_id, prev in app['redis_live'].ihscan('last_seen'):
prev = datetime.fromtimestamp(float(prev), tzutc())
if now - prev > timeout:
await app['event_dispatcher'].produce_event(
'instance_terminated', ('agent-lost', ),
agent_id=agent_id)
await redis.execute_with_retries(lambda: _check_impl())
except asyncio.CancelledError:
pass
# NOTE: This event is ignored during the grace period.
示例13
def shutdown(app: web.Application) -> None:
app['agent_lost_checker'].cancel()
await app['agent_lost_checker']
app['stats_task'].cancel()
await app['stats_task']
checked_tasks = ('kernel_agent_event_collector', 'kernel_ddtimer')
for tname in checked_tasks:
t = app.get(tname, None)
if t and not t.done():
t.cancel()
await t
for task in app['pending_waits']:
task.cancel()
try:
await task
except asyncio.CancelledError:
pass
示例14
def upstream(self):
try:
async for msg in self.down_conn:
if msg.type in (web.WSMsgType.TEXT, web.WSMsgType.binary):
await self.write(msg.data, msg.type)
if self.upstream_cb is not None:
await self.upstream_cb(msg.data)
elif msg.type == web.WSMsgType.PING:
if self.ping_cb is not None:
await self.ping_cb(msg.data)
elif msg.type == aiohttp.WSMsgType.ERROR:
log.error("ws connection closed with exception {}",
self.up_conn.exception())
break
elif msg.type == aiohttp.WSMsgType.CLOSE:
break
# here, client gracefully disconnected
except asyncio.CancelledError:
# here, client forcibly disconnected
raise
finally:
await self.close_downstream()
示例15
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()
示例16
def dispatch_subscribers(self, event_name: str, agent_id: AgentId,
args: Tuple[Any, ...] = tuple()) -> None:
log_fmt = 'DISPATCH_SUBSCRIBERS(ev:{}, ag:{})'
log_args = (event_name, agent_id)
if self.root_app['config']['debug']['log-events']:
log.debug(log_fmt, *log_args)
scheduler = get_scheduler_from_app(self.root_app)
for subscriber in self.subscribers[event_name]:
cb = subscriber.callback
try:
if asyncio.iscoroutine(cb):
await scheduler.spawn(cb)
elif asyncio.iscoroutinefunction(cb):
await scheduler.spawn(cb(subscriber.context, agent_id, event_name, *args))
else:
cb = functools.partial(cb, subscriber.context, agent_id, event_name, *args)
self.loop.call_soon(cb)
except asyncio.CancelledError:
raise
except Exception:
log.exception(log_fmt + ': unexpected-error', *log_args)
示例17
def _subscribe(self) -> None:
async def _subscribe_impl():
channels = await self.redis_subscriber.subscribe('events.pubsub')
async for raw_msg in channels[0].iter():
msg = msgpack.unpackb(raw_msg)
await self.dispatch_subscribers(msg['event_name'],
msg['agent_id'],
msg['args'])
while True:
try:
await redis.execute_with_retries(lambda: _subscribe_impl())
except asyncio.CancelledError:
break
except Exception:
log.exception('EventDispatcher.subscribe(): unexpected-error')
示例18
def handle_request(self, request: Request, *, _preserve: bool = False) -> Response:
async with self.request_context(request, _preserve=_preserve) as request_context:
try:
return await self.full_dispatch_request(request_context)
except asyncio.CancelledError:
raise # CancelledErrors should be handled by serving code.
except Exception as error:
return await self.handle_exception(error)
示例19
def handle_websocket(
self, websocket: Websocket, *, _preserve: bool = False
) -> Optional[Response]:
async with self.websocket_context(websocket, _preserve=_preserve) as websocket_context:
try:
return await self.full_dispatch_websocket(websocket_context)
except asyncio.CancelledError:
raise # CancelledErrors should be handled by serving code.
except Exception as error:
return await self.handle_websocket_exception(error)
示例20
def test_app_handle_websocket_asyncio_cancelled_error() -> None:
app = Quart(__name__)
@app.websocket("/")
async def index() -> NoReturn:
raise asyncio.CancelledError()
websocket = app.websocket_class(
"/", b"", "wss", Headers([("host", "quart.com")]), "", "1.1", None, None, None, None
)
with pytest.raises(asyncio.CancelledError):
await app.handle_websocket(websocket)
示例21
def handler_cancelled(request, exception):
# If we get a CancelledError, it means sanic has already sent a response,
# we should not ever have to handle a CancelledError.
response_handler_cancelled_app.flag = True
return text("App received CancelledError!", 500)
# The client will never receive this response, because the socket
# is already closed when we get a CancelledError.
示例22
def test_request_cancel_when_connection_lost(app):
app.still_serving_cancelled_request = False
@app.get("/")
async def handler(request):
await asyncio.sleep(1.0)
# at this point client is already disconnected
app.still_serving_cancelled_request = True
return text("OK")
# schedule client call
loop = asyncio.get_event_loop()
task = loop.create_task(app.asgi_client.get("/"))
loop.call_later(0.01, task)
await asyncio.sleep(0.5)
# cancelling request and closing connection after 0.5 sec
task.cancel()
with contextlib.suppress(asyncio.CancelledError):
await task
# Wait for server and check if it's still serving the cancelled request
await asyncio.sleep(1.0)
assert app.still_serving_cancelled_request is False
示例23
def test_stream_request_cancel_when_conn_lost(app):
app.still_serving_cancelled_request = False
@app.post("/post/<id>", stream=True)
async def post(request, id):
assert isinstance(request.stream, asyncio.Queue)
async def streaming(response):
while True:
body = await request.stream.get()
if body is None:
break
await response.write(body.decode("utf-8"))
await asyncio.sleep(1.0)
# at this point client is already disconnected
app.still_serving_cancelled_request = True
return stream(streaming)
# schedule client call
loop = asyncio.get_event_loop()
task = loop.create_task(app.asgi_client.post("/post/1"))
loop.call_later(0.01, task)
await asyncio.sleep(0.5)
# cancelling request and closing connection after 0.5 sec
task.cancel()
with contextlib.suppress(asyncio.CancelledError):
await task
# Wait for server and check if it's still serving the cancelled request
await asyncio.sleep(1.0)
assert app.still_serving_cancelled_request is False
示例24
def _websocket_handler(
self, handler, request, *args, subprotocols=None, **kwargs
):
request.app = self
if not getattr(handler, "__blueprintname__", False):
request.endpoint = handler.__name__
else:
request.endpoint = (
getattr(handler, "__blueprintname__", "") + handler.__name__
)
pass
if self.asgi:
ws = request.transport.get_websocket_connection()
else:
protocol = request.transport.get_protocol()
protocol.app = self
ws = await protocol.websocket_handshake(request, subprotocols)
# schedule the application handler
# its future is kept in self.websocket_tasks in case it
# needs to be cancelled due to the server being stopped
fut = ensure_future(handler(request, ws, *args, **kwargs))
self.websocket_tasks.add(fut)
try:
await fut
except (CancelledError, ConnectionClosed):
pass
finally:
self.websocket_tasks.remove(fut)
await ws.close()
# -------------------------------------------------------------------- #
# ASGI
# -------------------------------------------------------------------- #
示例25
def send_data(self, data, stream_id):
"""
Send data according to the flow control rules.
"""
while data:
while self.conn.local_flow_control_window(stream_id) < 1:
try:
await self.wait_for_flow_control(stream_id)
except asyncio.CancelledError:
return
chunk_size = min(
self.conn.local_flow_control_window(stream_id),
len(data),
self.conn.max_outbound_frame_size,
)
try:
self.conn.send_data(
stream_id,
data[:chunk_size],
end_stream=(chunk_size == len(data))
)
except (StreamClosedError, ProtocolError):
# The stream got closed and we didn't get told. We're done
# here.
break
self.transport.write(self.conn.data_to_send())
data = data[chunk_size:]
示例26
def start(self, request_handler):
self._handler = request_handler
self._server = await self._loop.create_server(
lambda: _HTTPServerProtocol(parent=self, loop=self._loop),
host='0.0.0.0',
port=self.port,
reuse_address=True)
print(f'-- Listening for HTTP on port {self.port} --')
try:
await self._done_future
except asyncio.CancelledError:
pass
logger.warning("Shutting down HTTP transport")
await asyncio.sleep(self.shutdown_wait_period)
# wait for connections to stop
times_no_connections = 0
for _ in range(self.shutdown_grace_period):
if not self._connections:
times_no_connections += 1
else:
times_no_connections = 0
for con in self._connections:
con.attempt_close()
if times_no_connections > 3:
# three seconds with no connections
break
await asyncio.sleep(1)
# Shut the server down
self._server.close()
await self._server.wait_closed()
示例27
def process(self, queue, workflow):
try:
while queue.__futures__:
done, _ = await wait(queue.__futures__,
return_when=FIRST_COMPLETED)
queue.progress(done)
return workflow.result()
except CancelledError:
for task in queue.__futures__:
task.cancel()
await gather(*queue.__futures__)
raise
示例28
def is_object(self, key, bucket=None):
""" Return true if the given object exists
"""
if not bucket:
log.error("is_object - bucket not set")
raise HTTPInternalServerError()
start_time = time.time()
found = False
try:
async with self._client.get_blob_client(container=bucket, blob=key) as blob_client:
blob_props = await blob_client.get_blob_properties()
if blob_props:
found = True
finish_time = time.time()
except CancelledError as cle:
self._azure_stats_increment("error_count")
msg = f"azureBlobClient.CancelledError get_blob_properties {key}: {cle}"
log.error(msg)
raise HTTPInternalServerError()
except Exception as e:
if isinstance(e, AzureError):
if e.status_code == 404:
msg = f"storage key: {key} not found "
log.warn(msg)
finish_time = time.time()
elif e.status_code in (401, 403):
msg = f"azureBlobClient.access denied for get_blob_properties key: {key}"
log.info(msg)
raise HTTPForbidden()
else:
self._azure_stats_increment("error_count")
log.error(f"azureBlobClient.got unexpected AzureError for get_blob_properties {key}: {e.message}")
raise HTTPInternalServerError()
else:
log.error(f"azureBlobClient.Unexpected exception for get_blob_properties {key}: {e}")
raise HTTPInternalServerError()
log.info(f"azureBlobClient.is_object({key} bucket={bucket}) start={start_time:.4f} finish={finish_time:.4f} elapsed={finish_time-start_time:.4f}")
return found
示例29
def http_post(app, url, data=None, params=None):
log.info(f"http_post('{url}', {data})")
client = get_http_client(app)
rsp_json = None
timeout = config.get("timeout")
try:
async with client.post(url, json=data, params=params, timeout=timeout ) as rsp:
log.info(f"http_post status: {rsp.status}")
if rsp.status == 200:
pass # ok
elif rsp.status == 201:
pass # also ok
elif rsp.status == 204: # no data
return None
elif rsp.status == 404:
log.info(f"POST reqest HTTPNotFound error for url: {url}")
elif rsp.status == 410:
log.info(f"POST reqest HTTPGone error for url: {url}")
elif rsp.status == 503:
log.warn(f"503 error for http_get_Json {url}")
raise HTTPServiceUnavailable()
else:
log.warn(f"POST request error for url: {url} - status: {rsp.status}")
raise HTTPInternalServerError()
rsp_json = await rsp.json()
log.debug(f"http_post({url}) response: {rsp_json}")
except ClientError as ce:
log.error(f"Error for http_post({url}): {ce} ")
raise HTTPInternalServerError()
except CancelledError as cle:
log.error(f"CancelledError for http_post({url}): {cle}")
raise HTTPInternalServerError()
return rsp_json
示例30
def http_put(app, url, data=None, params=None):
log.info(f"http_put('{url}', data: {data})")
rsp = None
client = get_http_client(app)
timeout = config.get("timeout")
try:
async with client.put(url, json=data, params=params, timeout=timeout) as rsp:
log.info(f"http_put status: {rsp.status}")
if rsp.status == 201:
pass # expected
elif rsp.status == 404:
# can come up for replace ops
log.info(f"HTTPNotFound for: {url}")
elif rsp.status == 409:
log.info(f"HTTPConflict for: {url}")
raise HTTPConflict()
elif rsp.status == 503:
log.warn(f"503 error for http_put url: {url}")
raise HTTPServiceUnavailable()
else:
log.error(f"PUT request error for url: {url} - status: {rsp.status}")
raise HTTPInternalServerError()
rsp_json = await rsp.json()
log.debug(f"http_put({url}) response: {rsp_json}")
except ClientError as ce:
log.error(f"ClientError for http_put({url}): {ce} ")
raise HTTPInternalServerError()
except CancelledError as cle:
log.error(f"CancelledError for http_put({url}): {cle}")
raise HTTPInternalServerError()
return rsp_json