|
5 | 5 | import asyncio |
6 | 6 | import logging |
7 | 7 | import socket |
8 | | -import time |
9 | | -from typing import List |
| 8 | +from typing import List, Optional |
10 | 9 |
|
11 | 10 | from zeroconf import IPVersion |
12 | 11 | from zeroconf.asyncio import AsyncServiceInfo, AsyncZeroconf |
13 | 12 |
|
14 | 13 |
|
15 | | -async def register_services(infos: List[AsyncServiceInfo]) -> None: |
16 | | - tasks = [aiozc.async_register_service(info) for info in infos] |
17 | | - background_tasks = await asyncio.gather(*tasks) |
18 | | - await asyncio.gather(*background_tasks) |
19 | | - |
20 | | - |
21 | | -async def unregister_services(infos: List[AsyncServiceInfo]) -> None: |
22 | | - tasks = [aiozc.async_unregister_service(info) for info in infos] |
23 | | - background_tasks = await asyncio.gather(*tasks) |
24 | | - await asyncio.gather(*background_tasks) |
| 14 | +class AsyncRunner: |
| 15 | + def __init__(self, ip_version: IPVersion) -> None: |
| 16 | + self.ip_version = ip_version |
| 17 | + self.aiozc: Optional[AsyncZeroconf] = None |
25 | 18 |
|
| 19 | + async def register_services(self, infos: List[AsyncServiceInfo]) -> None: |
| 20 | + self.aiozc = AsyncZeroconf(ip_version=self.ip_version) |
| 21 | + tasks = [self.aiozc.async_register_service(info) for info in infos] |
| 22 | + background_tasks = await asyncio.gather(*tasks) |
| 23 | + await asyncio.gather(*background_tasks) |
| 24 | + print("Finished registration, press Ctrl-C to exit...") |
| 25 | + while True: |
| 26 | + await asyncio.sleep(1) |
26 | 27 |
|
27 | | -async def close_aiozc(aiozc: AsyncZeroconf) -> None: |
28 | | - await aiozc.async_close() |
| 28 | + async def unregister_services(self, infos: List[AsyncServiceInfo]) -> None: |
| 29 | + assert self.aiozc is not None |
| 30 | + tasks = [self.aiozc.async_unregister_service(info) for info in infos] |
| 31 | + background_tasks = await asyncio.gather(*tasks) |
| 32 | + await asyncio.gather(*background_tasks) |
| 33 | + await self.aiozc.async_close() |
29 | 34 |
|
30 | 35 |
|
31 | 36 | if __name__ == '__main__': |
@@ -60,18 +65,10 @@ async def close_aiozc(aiozc: AsyncZeroconf) -> None: |
60 | 65 | ) |
61 | 66 | ) |
62 | 67 |
|
63 | | - print("Registration of 250 services, press Ctrl-C to exit...") |
64 | | - aiozc = AsyncZeroconf(ip_version=ip_version) |
| 68 | + print("Registration of 250 services...") |
65 | 69 | loop = asyncio.get_event_loop() |
66 | | - loop.run_until_complete(register_services(infos)) |
67 | | - print("Registration complete.") |
| 70 | + runner = AsyncRunner(ip_version) |
68 | 71 | try: |
69 | | - while True: |
70 | | - time.sleep(0.1) |
| 72 | + loop.run_until_complete(runner.register_services(infos)) |
71 | 73 | except KeyboardInterrupt: |
72 | | - pass |
73 | | - finally: |
74 | | - print("Unregistering...") |
75 | | - loop.run_until_complete(unregister_services(infos)) |
76 | | - print("Unregistration complete.") |
77 | | - loop.run_until_complete(close_aiozc(aiozc)) |
| 74 | + loop.run_until_complete(runner.unregister_services(infos)) |
0 commit comments