本文整理汇总了Python中trio.sleep方法的典型用法代码示例。如果您正苦于以下问题:Python trio.sleep方法的具体用法?Python trio.sleep怎么用?Python trio.sleep使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类trio
的用法示例。
在下文中一共展示了trio.sleep方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: log_retries
# 需要导入模块: import trio [as 别名]
# 或者: from trio import sleep [as 别名]
def log_retries(logger):
def log_it(retry_obj, sleep, last_result):
level = min(
[logging.WARNING, (10 * retry_obj.statistics.get("attempt_number", 1))]
)
reason_text = "exception" if last_result.failed else "result"
reason_value = (
last_result.exception() if last_result.failed else last_result.result()
)
logger.log(
level,
"Retrying %s in %2d seconds (attempt %2d) due to %s: %r.",
retry_obj.fn.__qualname__,
sleep,
retry_obj.statistics.get("attempt_number", None),
reason_text,
reason_value,
)
return log_it
#
# I/O Functions
#
示例2: test_multiple_contexts
# 需要导入模块: import trio [as 别名]
# 或者: from trio import sleep [as 别名]
def test_multiple_contexts():
async def recv_and_send(ctx):
data = await ctx.arecv()
await trio.sleep(0.05)
await ctx.asend(data)
with pynng.Rep0(listen=addr, recv_timeout=500) as rep, \
pynng.Req0(dial=addr, recv_timeout=500) as req1, \
pynng.Req0(dial=addr, recv_timeout=500) as req2:
async with trio.open_nursery() as n:
ctx1, ctx2 = [rep.new_context() for _ in range(2)]
with ctx1, ctx2:
n.start_soon(recv_and_send, ctx1)
n.start_soon(recv_and_send, ctx2)
await req1.asend(b'oh hi')
await req2.asend(b'me toooo')
assert (await req1.arecv() == b'oh hi')
assert (await req2.arecv() == b'me toooo')
示例3: test_pair1_polyamorousness
# 需要导入模块: import trio [as 别名]
# 或者: from trio import sleep [as 别名]
def test_pair1_polyamorousness():
with pynng.Pair1(listen=addr, polyamorous=True, recv_timeout=500) as s0, \
pynng.Pair1(dial=addr, polyamorous=True, recv_timeout=500) as s1:
wait_pipe_len(s0, 1)
# pipe for s1 .
p1 = s0.pipes[0]
with pynng.Pair1(dial=addr, polyamorous=True, recv_timeout=500) as s2:
wait_pipe_len(s0, 2)
# pipes is backed by a dict, so we can't rely on order in
# Python 3.5.
pipes = s0.pipes
p2 = pipes[1]
if p2 is p1:
p2 = pipes[0]
p1.send(b'hello s1')
assert s1.recv() == b'hello s1'
p2.send(b'hello there s2')
assert s2.recv() == b'hello there s2'
# TODO: Should *not* need to do this sleep, but stuff hangs without
# it.
time.sleep(0.05)
示例4: slow_framework
# 需要导入模块: import trio [as 别名]
# 或者: from trio import sleep [as 别名]
def slow_framework(scope: dict, receive: Callable, send: Callable) -> None:
while True:
event = await receive()
if event["type"] == "http.disconnect":
break
elif event["type"] == "lifespan.startup":
await send({"type": "lifspan.startup.complete"})
elif event["type"] == "lifespan.shutdown":
await send({"type": "lifspan.shutdown.complete"})
elif event["type"] == "http.request" and not event.get("more_body", False):
await trio.sleep(2 * KEEP_ALIVE_TIMEOUT)
await send(
{
"type": "http.response.start",
"status": 200,
"headers": [(b"content-length", b"0")],
}
)
await send({"type": "http.response.body", "body": b"", "more_body": False})
break
示例5: test_http1_keep_alive
# 需要导入模块: import trio [as 别名]
# 或者: from trio import sleep [as 别名]
def test_http1_keep_alive(
client_stream: trio.testing._memory_streams.MemorySendStream,
) -> None:
client = h11.Connection(h11.CLIENT)
await client_stream.send_all(client.send(REQUEST))
await trio.sleep(2 * KEEP_ALIVE_TIMEOUT)
await client_stream.send_all(client.send(h11.EndOfMessage()))
while True:
event = client.next_event()
if event == h11.NEED_DATA:
data = await client_stream.receive_some(2 ** 16)
client.receive_data(data)
elif isinstance(event, h11.EndOfMessage):
break
client.start_next_cycle()
await client_stream.send_all(client.send(REQUEST))
await trio.sleep(2 * KEEP_ALIVE_TIMEOUT)
# Key is that this doesn't error
await client_stream.send_all(client.send(h11.EndOfMessage()))
示例6: _wait_for_winfsp_ready
# 需要导入模块: import trio [as 别名]
# 或者: from trio import sleep [as 别名]
def _wait_for_winfsp_ready(mountpoint_path, timeout=1.0):
trio_mountpoint_path = trio.Path(mountpoint_path)
# Polling for `timeout` seconds until winfsp is ready
with trio.fail_after(timeout):
while True:
try:
if await trio_mountpoint_path.exists():
return
await trio.sleep(0.01)
# Looks like a revoked workspace has been mounted
except PermissionError:
return
# Might be another OSError like errno 113 (No route to host)
except OSError:
return
示例7: test_waiter_on
# 需要导入模块: import trio [as 别名]
# 或者: from trio import sleep [as 别名]
def test_waiter_on(event_bus):
async def _trigger(id):
await trio.sleep(0)
event_bus.send("foo", id=id)
async with trio.open_service_nursery() as nursery:
with event_bus.waiter_on("foo") as waiter:
nursery.start_soon(_trigger, 1)
event = await waiter.wait()
assert event == ("foo", {"id": 1})
# Event is ignored
event_bus.send("foo", id=2)
same_event = await waiter.wait()
assert same_event == event
waiter.clear()
nursery.start_soon(_trigger, 3)
event = await waiter.wait()
assert event == ("foo", {"id": 3})
assert event_bus.stats() == {}
示例8: test_waiter_on_first
# 需要导入模块: import trio [as 别名]
# 或者: from trio import sleep [as 别名]
def test_waiter_on_first(event_bus):
async def _trigger(event, id):
await trio.sleep(0)
event_bus.send(event, id=id)
async with trio.open_service_nursery() as nursery:
with event_bus.waiter_on_first("foo", "bar") as waiter:
nursery.start_soon(_trigger, "foo", 1)
event = await waiter.wait()
assert event == ("foo", {"id": 1})
# Event is ignored
event_bus.send("bar", id=2)
same_event = await waiter.wait()
assert same_event == event
waiter.clear()
nursery.start_soon(_trigger, "bar", 3)
event = await waiter.wait()
assert event == ("bar", {"id": 3})
assert event_bus.stats() == {}
示例9: test_device_invite_same_name_different_organizations
# 需要导入模块: import trio [as 别名]
# 或者: from trio import sleep [as 别名]
def test_device_invite_same_name_different_organizations(
backend, apiv1_alice_backend_sock, apiv1_otheralice_backend_sock, alice, otheralice, alice_nd_id
):
with backend.event_bus.listen() as spy, trio.fail_after(1):
async with device_invite(
apiv1_alice_backend_sock, invited_device_name=alice_nd_id.device_name
) as prep:
await spy.wait(MetaEvent.EVENT_CONNECTED, {"event_type": BackendEvent.DEVICE_CLAIMED})
backend.event_bus.send(
BackendEvent.DEVICE_CLAIMED,
organization_id=otheralice.organization_id,
device_id=alice_nd_id,
encrypted_claim=b"<from OtherOrg>",
)
await trio.sleep(0)
backend.event_bus.send(
BackendEvent.DEVICE_CLAIMED,
organization_id=alice.organization_id,
device_id=alice_nd_id,
encrypted_claim=b"<from CoolOrg>",
)
assert prep[0] == {"status": "ok", "encrypted_claim": b"<from CoolOrg>"}
示例10: test_raid5_block_create_single_failure
# 需要导入模块: import trio [as 别名]
# 或者: from trio import sleep [as 别名]
def test_raid5_block_create_single_failure(
caplog, alice_backend_sock, backend, realm, failing_blockstore
):
async def mock_create(organization_id, id, block):
await trio.sleep(0)
raise BlockTimeoutError()
backend.blockstore.blockstores[failing_blockstore].create = mock_create
await block_create(alice_backend_sock, BLOCK_ID, realm, BLOCK_DATA)
# Should be notified of blockstore malfunction
caplog.assert_occured(
f"[warning ] Cannot reach RAID5 blockstore #{failing_blockstore} to "
f"create block {BLOCK_ID} [parsec.backend.raid5_blockstore]"
)
示例11: test_raid5_block_read_single_failure
# 需要导入模块: import trio [as 别名]
# 或者: from trio import sleep [as 别名]
def test_raid5_block_read_single_failure(
caplog, alice_backend_sock, alice, backend, block, failing_blockstore
):
async def mock_read(organization_id, id):
await trio.sleep(0)
raise BlockTimeoutError()
backend.blockstore.blockstores[failing_blockstore].read = mock_read
rep = await block_read(alice_backend_sock, block)
assert rep == {"status": "ok", "block": BLOCK_DATA}
# Should be notified of blockstore malfunction
caplog.assert_occured(
f"[warning ] Cannot reach RAID5 blockstore #{failing_blockstore} to "
f"read block {block} [parsec.backend.raid5_blockstore]"
)
示例12: test_monitor_crash
# 需要导入模块: import trio [as 别名]
# 或者: from trio import sleep [as 别名]
def test_monitor_crash(running_backend, event_bus, alice, during_bootstrap):
async def _bad_monitor(*, task_status=trio.TASK_STATUS_IGNORED):
if during_bootstrap:
raise RuntimeError("D'oh !")
task_status.started()
await trio.sleep(0)
raise RuntimeError("D'oh !")
conn = BackendAuthenticatedConn(
alice.organization_addr, alice.device_id, alice.signing_key, event_bus
)
with event_bus.listen() as spy:
conn.register_monitor(_bad_monitor)
async with conn.run():
await spy.wait_with_timeout(
CoreEvent.BACKEND_CONNECTION_CHANGED,
{"status": BackendConnStatus.CRASHED, "status_exc": spy.ANY},
)
assert conn.status == BackendConnStatus.CRASHED
# Test command not possible
with pytest.raises(BackendNotAvailable) as exc:
await conn.cmds.ping()
assert str(exc.value) == "Backend connection manager has crashed: D'oh !"
示例13: receive_all
# 需要导入模块: import trio [as 别名]
# 或者: from trio import sleep [as 别名]
def receive_all(self, stream: trio.abc.Stream, length=1024, bsize=None):
buf = b''
bsize = bsize if bsize is not None else 1024
bsize = min(bsize, length)
received = 0
tries = 0
while received < length:
newbuf = await stream.receive_some(bsize)
if not newbuf:
# 3 successive read failure => raise exception
if tries > 3:
raise IOError('Unable to read full response')
tries += 1
await trio.sleep(0.1)
continue
# reset counter
tries = 0
buf += newbuf
received += len(newbuf)
return buf
示例14: receive_all
# 需要导入模块: import trio [as 别名]
# 或者: from trio import sleep [as 别名]
def receive_all(self, stream: trio.StapledStream, length=1024, bsize=None):
buf = b''
bsize = bsize if bsize is not None else 1024
bsize = min(bsize, length)
received = 0
tries = 0
while received < length:
newbuf = await stream.receive_some(bsize)
if not newbuf:
# 3 successive read failure => raise exception
if tries > 3:
raise Exception('Unable to read full response')
tries += 1
await trio.sleep(0.1)
continue
# reset counter
tries = 0
buf += newbuf
received += len(newbuf)
return buf
示例15: receive_all
# 需要导入模块: import trio [as 别名]
# 或者: from trio import sleep [as 别名]
def receive_all(self, length=1024, bsize=None):
buf = b''
bsize = bsize if bsize is not None else 1024
bsize = min(bsize, length)
received = 0
tries = 0
while received < length:
newbuf = await self.client_stream.receive_some(bsize)
if not newbuf:
# 3 successive read failure => raise exception
if tries > 3:
raise IOError('Unable to read full response')
tries += 1
trio.sleep(0.1)
continue
# reset counter
tries = 0
buf += newbuf
received += len(newbuf)
return buf
# Some functions are the same as in Burp1 backend