ch22: examples

This commit is contained in:
Luciano Ramalho
2021-02-28 21:19:40 -03:00
parent d2bcf655d7
commit a751c86836
10 changed files with 107 additions and 74 deletions

View File

@@ -0,0 +1,25 @@
import asyncio
import socket
from collections.abc import Iterable, AsyncIterator
from typing import NamedTuple
class Result(NamedTuple):
name: str
found: bool
async def probe(loop: asyncio.AbstractEventLoop, name: str) -> Result:
try:
await loop.getaddrinfo(name, None)
except socket.gaierror:
return Result(name, False)
return Result(name, True)
async def multi_probe(names: Iterable[str]) -> AsyncIterator[Result]:
loop = asyncio.get_running_loop()
coros = [probe(loop, name) for name in names]
for coro in asyncio.as_completed(coros):
result = await coro
yield result