fluentpython / example-code-2e

Example code for Fluent Python, 2nd edition (O'Reilly 2022)
https://amzn.to/3J48u2J
MIT License
3.17k stars 902 forks source link

Why using synchronous get in flags_asyncio.py #40

Open ngokchaoho opened 9 months ago

ngokchaoho commented 9 months ago

When downloading the flags in

async def get_flag(client: AsyncClient, cc: str) -> bytes:  4
    url = f'{BASE_URL}/{cc}/{cc}.gif'.lower()
    resp = await client.get(url, timeout=6.1,
                                  follow_redirects=True)  5
    return resp.read()  6

it seems that we read the response using .read() which is synchronous, why don't we

return await resp.aread()

instead?

The explanation seems to be for client.get() where it is doing network IO asynchrnously provided by httpx. But not for read()

6

    Network I/O operations are implemented as coroutine methods, so they are driven asynchronously by the asyncio event loop.