Hacker News new | ask | show | jobs
by api 1011 days ago
Awaits don’t create threads, at least not in any runtime I know of. There is usually a fixed number of threads at launch.
4 comments

FastAPI docs, case when you don't create an async route

> When you declare a path operation function with normal def instead of async def, it is run in an external threadpool that is then awaited, instead of being called directly (as it would block the server).

https://fastapi.tiangolo.com/async/#path-operation-functions

OP either meant this, or its variation, such as async_to_sync and sync_to_async. https://github.com/django/asgiref/blob/main/asgiref/sync.py

Ofc this is a python example. I have no idea how it works in different languages.

“run in a threadpool” isn’t the same as creating a thread though
NB: In Python >= 3.9 the idiomatic way to do this is to_thread(), not familiar with these ASGI functions but I would guess they're a polyfill and/or predate 3.9.

https://docs.python.org/3/library/asyncio-task.html#asyncio....

They are not polyfills. Multiple scheduling modes are provided for libraries that are not thread safe (it's a total mess and I avoid these wrappers like the plague)
I've had to weld some async and sync Python together with queues and callbacks, it's not pretty.
That is an implementation detail on where you put the code that is blocking or running concurrently from the main code. An executor could use a separate OS thread, or the application could itself schedule application levels threads onto a number of OS threads.

When writing a Future that will block for 5 seconds you will need to find somewhere to that you can put the code to block for 5 seconds. You don't technically need to even use an executor here.

I think they meant it was likely to spin off additional tasks/green threads.
If they meant that they are still wrong.
Tokio uses a pool of threads for disk I/O because it uses the synchronous calls of the operating system.