本文整理汇总了Python中trio.BrokenResourceError方法的典型用法代码示例。如果您正苦于以下问题:Python trio.BrokenResourceError方法的具体用法?Python trio.BrokenResourceError怎么用?Python trio.BrokenResourceError使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类trio
的用法示例。
在下文中一共展示了trio.BrokenResourceError方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: switch_offline
# 需要导入模块: import trio [as 别名]
# 或者: from trio import BrokenResourceError [as 别名]
def switch_offline(self, addr):
netloc_suffix = addr_to_netloc(addr)
if netloc_suffix in self._offlines:
return
for netloc, sock in self._socks:
if not netloc.endswith(netloc_suffix):
continue
async def _broken_stream(*args, **kwargs):
raise trio.BrokenResourceError()
_broken_stream.old_send_all_hook = sock.send_stream.send_all_hook
_broken_stream.old_receive_some_hook = sock.receive_stream.receive_some_hook
sock.send_stream.send_all_hook = _broken_stream
sock.receive_stream.receive_some_hook = _broken_stream
# Unlike for send stream, patching `receive_some_hook` is not enough
# because it is called by the stream before actually waiting for data
# (so in case of a long receive call, we could be past the hook call
# when patching, hence not blocking the next incoming frame)
sock.receive_stream.put_eof()
self._offlines.add(netloc_suffix)
示例2: download_file
# 需要导入模块: import trio [as 别名]
# 或者: from trio import BrokenResourceError [as 别名]
def download_file(self, url, headers=IPHONE_HEADER, timeout=DOWNLOAD_TIMEOUT, res_time=RETRIES_TIMES):
if res_time <= 0: # 重试超过了次数
return None
try:
_url = random.choice(url) if isinstance(url, list) else url
res = await asks.get(_url, headers=headers, timeout=timeout, retries=3)
except (socket.gaierror, trio.BrokenResourceError, trio.TooSlowError, asks.errors.RequestTimeout) as e:
logging.error("download from %s fail]err=%s!" % (url, e))
await trio.sleep(random.randint(1, 5)) # for scheduler
return await self.download_file(url, res_time=res_time-1)
if res.status_code not in [200, 202]:
logging.warn(f"download from {url} fail]response={res}")
await trio.sleep(random.randint(3, 10))
return await self.download_file(url, res_time=res_time-1)
return res.content
示例3: run
# 需要导入模块: import trio [as 别名]
# 或者: from trio import BrokenResourceError [as 别名]
def run(self):
"""
Run the subscription.
:returns: This function returns when the sync is complete.
"""
logger.info("%r Starting", self)
async with trio.open_nursery() as nursery:
self._cancel_scope = nursery.cancel_scope
await self._set_initial_job_status()
nursery.start_soon(self._job_status_task)
try:
await self._run_sync()
except (trio.BrokenResourceError, trio.ClosedResourceError):
logger.info("%r Aborted", self)
nursery.cancel_scope.cancel()
try:
await self._send_complete()
logger.info("%r Finished", self)
except (trio.BrokenResourceError, trio.ClosedResourceError):
# If we can't send the completion message, then bail out.
pass
示例4: _send_job_state_event
# 需要导入模块: import trio [as 别名]
# 或者: from trio import BrokenResourceError [as 别名]
def _send_job_state_event(self, event):
'''
Send a job state event to all listeners.
If a listener's channel is full, then the event is not sent to that
listener. If the listener's channel is closed, then that channel is
removed and will not be sent to in the future.
:param JobStateEvent event:
'''
to_remove = list()
for recv_channel, send_channel in self._job_state_channels.items():
try:
send_channel.send_nowait(event)
except trio.WouldBlock:
# The channel is full. Drop this message and move on.
pass
except trio.BrokenResourceError:
# The consumer closed the channel. We can remove it from our
# dict after the loop finishes.
to_remove.append(recv_channel)
for recv_channel in to_remove:
del self._job_state_channels[recv_channel]
示例5: _send
# 需要导入模块: import trio [as 别名]
# 或者: from trio import BrokenResourceError [as 别名]
def _send(self, event):
'''
Send an to the remote WebSocket.
The reader task and one or more writers might try to send messages at
the same time, so this method uses an internal lock to serialize
requests to send data.
:param wsproto.events.Event event:
'''
data = self._wsproto.send(event)
async with self._stream_lock:
logger.debug('%s sending %d bytes', self, len(data))
try:
await self._stream.send_all(data)
except (trio.BrokenResourceError, trio.ClosedResourceError):
await self._abort_web_socket()
raise ConnectionClosed(self._close_reason) from None
示例6: recv_neighbours_v4
# 需要导入模块: import trio [as 别名]
# 或者: from trio import BrokenResourceError [as 别名]
def recv_neighbours_v4(self, remote: NodeAPI, payload: Sequence[Any], _: Hash32) -> None:
# The neighbours payload should have 2 elements: nodes, expiration
if len(payload) < 2:
self.logger.warning('Ignoring NEIGHBOURS msg with invalid payload: %s', payload)
return
nodes, expiration = payload[:2]
if self._is_msg_expired(expiration):
return
neighbours = _extract_nodes_from_payload(remote.address, nodes, self.logger)
self.logger.debug2('<<< neighbours from %s: %s', remote, neighbours)
try:
channel = self.neighbours_channels.get_channel(remote)
except KeyError:
self.logger.debug(f'unexpected neighbours from {remote}, probably came too late')
return
try:
await channel.send(neighbours)
except trio.BrokenResourceError:
# This means the receiver has already closed, probably because it timed out.
pass
示例7: run
# 需要导入模块: import trio [as 别名]
# 或者: from trio import BrokenResourceError [as 别名]
def run(self) -> None:
try:
try:
with trio.fail_after(self.config.ssl_handshake_timeout):
await self.stream.do_handshake()
except (trio.BrokenResourceError, trio.TooSlowError):
return # Handshake failed
alpn_protocol = self.stream.selected_alpn_protocol()
socket = self.stream.transport_stream.socket
ssl = True
except AttributeError: # Not SSL
alpn_protocol = "http/1.1"
socket = self.stream.socket
ssl = False
try:
client = parse_socket_addr(socket.family, socket.getpeername())
server = parse_socket_addr(socket.family, socket.getsockname())
async with trio.open_nursery() as nursery:
self.nursery = nursery
context = Context(nursery)
self.protocol = ProtocolWrapper(
self.app,
self.config,
context,
ssl,
client,
server,
self.protocol_send,
alpn_protocol,
)
await self.protocol.initiate()
await self._update_keep_alive_timeout()
await self._read_data()
except (trio.MultiError, OSError):
pass
finally:
await self._close()
示例8: protocol_send
# 需要导入模块: import trio [as 别名]
# 或者: from trio import BrokenResourceError [as 别名]
def protocol_send(self, event: Event) -> None:
if isinstance(event, RawData):
async with self.send_lock:
try:
with trio.CancelScope() as cancel_scope:
cancel_scope.shield = True
await self.stream.send_all(event.data)
except (trio.BrokenResourceError, trio.ClosedResourceError):
await self.protocol.handle(Closed())
elif isinstance(event, Closed):
await self._close()
await self.protocol.handle(Closed())
elif isinstance(event, Updated):
pass # Triggers the keep alive timeout update
await self._update_keep_alive_timeout()
示例9: _read_data
# 需要导入模块: import trio [as 别名]
# 或者: from trio import BrokenResourceError [as 别名]
def _read_data(self) -> None:
while True:
try:
data = await self.stream.receive_some(MAX_RECV)
except (trio.ClosedResourceError, trio.BrokenResourceError):
await self.protocol.handle(Closed())
break
else:
if data == b"":
await self._update_keep_alive_timeout()
break
await self.protocol.handle(RawData(data))
await self._update_keep_alive_timeout()
示例10: _close
# 需要导入模块: import trio [as 别名]
# 或者: from trio import BrokenResourceError [as 别名]
def _close(self) -> None:
try:
await self.stream.send_eof()
except (
trio.BrokenResourceError,
AttributeError,
trio.BusyResourceError,
trio.ClosedResourceError,
):
# They're already gone, nothing to do
# Or it is a SSL stream
pass
await self.stream.aclose()
示例11: test_http1_keep_alive_pre_request
# 需要导入模块: import trio [as 别名]
# 或者: from trio import BrokenResourceError [as 别名]
def test_http1_keep_alive_pre_request(
client_stream: trio.testing._memory_streams.MemorySendStream,
) -> None:
await client_stream.send_all(b"GET")
await trio.sleep(2 * KEEP_ALIVE_TIMEOUT)
# Only way to confirm closure is to invoke an error
with pytest.raises(trio.BrokenResourceError):
await client_stream.send_all(b"a")
示例12: _send
# 需要导入模块: import trio [as 别名]
# 或者: from trio import BrokenResourceError [as 别名]
def _send(self, data):
async with self._stream_lock:
try:
await self._stream.send_all(data)
except (trio.BrokenResourceError, trio.ClosedResourceError):
self._closed = True
示例13: _read_until
# 需要导入模块: import trio [as 别名]
# 或者: from trio import BrokenResourceError [as 别名]
def _read_until(self, delimiter):
""" Naive implementation of reading until a delimiter. """
buffer = bytearray()
try:
while True:
data = await self._stream.receive_some(1)
buffer.append(data[0])
if data == delimiter:
break
except (trio.BrokenResourceError, trio.ClosedResourceError):
self._closed = True
return bytes(buffer)
示例14: _read_exactly
# 需要导入模块: import trio [as 别名]
# 或者: from trio import BrokenResourceError [as 别名]
def _read_exactly(self, num):
data = b''
try:
while len(data) < num:
data += await self._stream.receive_some(num - len(data))
return data
except (trio.BrokenResourceError, trio.ClosedResourceError):
self._closed = True
示例15: close
# 需要导入模块: import trio [as 别名]
# 或者: from trio import BrokenResourceError [as 别名]
def close(self, noreply_wait=False, token=None, exception=None):
self._closing = True
if exception is not None:
err_message = "Connection is closed (%s)." % str(exception)
else:
err_message = "Connection is closed."
# Cursors may remove themselves when errored, so copy a list of them
for cursor in list(self._cursor_cache.values()):
cursor._error(err_message)
for _, future in self._user_queries.values():
if not future.done():
future.set_exception(ReqlDriverError(err_message))
self._user_queries = {}
self._cursor_cache = {}
if noreply_wait:
noreply = Query(P_QUERY.NOREPLY_WAIT, token, None, None)
await self.run_query(noreply, False)
try:
await self._stream.aclose()
except (trio.ClosedResourceError, trio.BrokenResourceError):
pass
# We must not wait for the _reader_task if we got an exception, because that
# means that we were called from it. Waiting would lead to a deadlock.
if self._reader_ended_event:
await self._reader_ended_event.wait()
return None