So, with Django 3.1 out all middlewares needs to be written to support async, so keeping it DRY is important.
I’ve got a bunch of simple util functions like these ones:
On my system, uuid.uuid4().hex runs in less than 0.00005 seconds (roughly 200,000 / second). I also can’t see where it’s performing any IO that would cause it to significantly block.
I’d have no problems running it in a blocking mode.
But that’s generally what you’d be looking for - is the function performing IO? Is it doing anything that could seriously affect the overall throughput? If so, then it’s (possibly) worth making it async-friendly or async-aware. If not, then there’s really nothing to be gained by doing it that way.
When I look at the docs at Async Support - HTTPX, all the examples show the requests being “await ed”.
Would you mind elaborating on your statement for me?
Yes, I’m talking about that library. It’s true that you await the request, but never the .json().
I think it’s easier to see if you look at the aiohttp docs. You can see that you have to await to read request data.
As a short comparison between httpx and aiohttp:
import asyncio
import httpx
import aiohttp
async def httpx_example():
async with httpx.AsyncClient() as client:
response = await client.get('https://swapi.dev/api/people/1')
# httpx do not need to await
json = response.json() # <--- No await
print(json)
async def aiohttp_example():
async with aiohttp.ClientSession() as session:
response = await session.get('https://swapi.dev/api/people/1')
json = await response.json() # <--- Await
print(json)
asyncio.run(httpx_example())
asyncio.run(aiohttp_example())
FYI, I’d put the response in a with-statement too, but was done to make it easy to compare. aiohttp code should probably look something like this:
import asyncio
import aiohttp
async def aiohttp_example():
async with aiohttp.ClientSession() as session:
async with session.get('https://swapi.dev/api/people/1') as response:
json = await response.json() # <--- Await
print(json)