|
| 1 | +import asyncio |
| 2 | + |
| 3 | +import ezmsg.core as ez |
| 4 | + |
| 5 | +PORT = 12345 |
| 6 | +MAX_COUNT = 100 |
| 7 | +TOPIC = "/TEST" |
| 8 | + |
| 9 | + |
| 10 | +async def handle_pub(pub: ez.Publisher) -> None: |
| 11 | + print("Publisher Task Launched") |
| 12 | + |
| 13 | + count = 0 |
| 14 | + |
| 15 | + while True: |
| 16 | + await pub.broadcast(f"{count=}") |
| 17 | + await asyncio.sleep(0.1) |
| 18 | + count += 1 |
| 19 | + if count >= MAX_COUNT: |
| 20 | + break |
| 21 | + |
| 22 | + print("Publisher Task Concluded") |
| 23 | + |
| 24 | + |
| 25 | +async def handle_sub(sub: ez.Subscriber) -> None: |
| 26 | + print("Subscriber Task Launched") |
| 27 | + |
| 28 | + rx_count = 0 |
| 29 | + while True: |
| 30 | + async with sub.recv_zero_copy() as msg: |
| 31 | + # Uncomment if you want to witness backpressure! |
| 32 | + # await asyncio.sleep(0.15) |
| 33 | + print(msg) |
| 34 | + |
| 35 | + rx_count += 1 |
| 36 | + if rx_count >= MAX_COUNT: |
| 37 | + break |
| 38 | + |
| 39 | + print("Subscriber Task Concluded") |
| 40 | + |
| 41 | + |
| 42 | +async def host(host: str = "127.0.0.1"): |
| 43 | + # Manually create a GraphServer |
| 44 | + server = ez.GraphServer() |
| 45 | + server.start((host, PORT)) |
| 46 | + |
| 47 | + print(f"Created GraphServer @ {server.address}") |
| 48 | + |
| 49 | + try: |
| 50 | + test_pub = await ez.Publisher.create(TOPIC, (host, PORT), host=host) |
| 51 | + test_sub1 = await ez.Subscriber.create(TOPIC, (host, PORT)) |
| 52 | + test_sub2 = await ez.Subscriber.create(TOPIC, (host, PORT)) |
| 53 | + |
| 54 | + await asyncio.sleep(1.0) |
| 55 | + |
| 56 | + pub_task = asyncio.Task(handle_pub(test_pub)) |
| 57 | + sub_task_1 = asyncio.Task(handle_sub(test_sub1)) |
| 58 | + sub_task_2 = asyncio.Task(handle_sub(test_sub2)) |
| 59 | + |
| 60 | + await asyncio.wait([pub_task, sub_task_1, sub_task_2]) |
| 61 | + |
| 62 | + test_pub.close() |
| 63 | + test_sub1.close() |
| 64 | + test_sub2.close() |
| 65 | + |
| 66 | + for future in asyncio.as_completed( |
| 67 | + [ |
| 68 | + test_pub.wait_closed(), |
| 69 | + test_sub1.wait_closed(), |
| 70 | + test_sub2.wait_closed(), |
| 71 | + ] |
| 72 | + ): |
| 73 | + await future |
| 74 | + |
| 75 | + finally: |
| 76 | + server.stop() |
| 77 | + |
| 78 | + print("Done") |
| 79 | + |
| 80 | + |
| 81 | +async def attach_client(host: str = "127.0.0.1"): |
| 82 | + |
| 83 | + sub = await ez.Subscriber.create(TOPIC, (host, PORT)) |
| 84 | + |
| 85 | + try: |
| 86 | + while True: |
| 87 | + async with sub.recv_zero_copy() as msg: |
| 88 | + # Uncomment if you want to see EXTREME backpressure! |
| 89 | + # await asyncio.sleep(1.0) |
| 90 | + print(msg) |
| 91 | + |
| 92 | + except asyncio.CancelledError: |
| 93 | + pass |
| 94 | + |
| 95 | + finally: |
| 96 | + sub.close() |
| 97 | + await sub.wait_closed() |
| 98 | + print("Detached") |
| 99 | + |
| 100 | + |
| 101 | +if __name__ == "__main__": |
| 102 | + from dataclasses import dataclass |
| 103 | + from argparse import ArgumentParser |
| 104 | + |
| 105 | + parser = ArgumentParser() |
| 106 | + parser.add_argument("--attach", action="store_true", help="attach to running graph") |
| 107 | + parser.add_argument("--host", default="0.0.0.0", help="hostname for graphserver") |
| 108 | + |
| 109 | + @dataclass |
| 110 | + class Args: |
| 111 | + attach: bool |
| 112 | + host: str |
| 113 | + |
| 114 | + args = Args(**vars(parser.parse_args())) |
| 115 | + |
| 116 | + if args.attach: |
| 117 | + asyncio.run(attach_client(host=args.host)) |
| 118 | + else: |
| 119 | + asyncio.run(host(host=args.host)) |
0 commit comments