-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnection_state.py
More file actions
41 lines (28 loc) · 1.1 KB
/
Copy pathconnection_state.py
File metadata and controls
41 lines (28 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
"""Wait for states and observe transitions of a connection state machine.
Run: python examples/connection_state.py
"""
import asyncio
from asyncio_util import AsyncValue
async def connect(state: AsyncValue[str]) -> None:
for next_state in ("connecting", "connected", "degraded", "connected"):
await asyncio.sleep(0.2)
print(f"[producer] -> {next_state}")
state.value = next_state
async def wait_until_connected(state: AsyncValue[str]) -> None:
value = await state.wait_value("connected")
print(f"[wait_value] connected (value={value!r})")
async def log_every_transition(state: AsyncValue[str]) -> None:
async for new, old in state.transitions():
print(f"[transitions] {old!r} -> {new!r}")
if new == "connected" and old == "degraded":
print("[transitions] recovered, stopping log")
return
async def main() -> None:
state = AsyncValue("disconnected")
await asyncio.gather(
wait_until_connected(state),
log_every_transition(state),
connect(state),
)
if __name__ == "__main__":
asyncio.run(main())