]> git.ipfire.org Git - thirdparty/starlette.git/commitdiff
Update note about anyio thread pool (#2895)
authorAdrian Garcia Badaracco <1755071+adriangb@users.noreply.github.com>
Sat, 8 Mar 2025 13:02:32 +0000 (07:02 -0600)
committerGitHub <noreply@github.com>
Sat, 8 Mar 2025 13:02:32 +0000 (13:02 +0000)
* Update note about anyio thread pool

* Improve a bit the first sentence

---------

Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
docs/threadpool.md

index eec3677eb9d88e0ddd76f47a0ab9028ca46c8360..09aed7162ece584f8ec61be9b5d62d5a464aa037 100644 (file)
@@ -1,14 +1,22 @@
 # Thread Pool
 
-When you're using `def` instead of `async def`, Starlette will run your code in a thread pool to avoid
-blocking the event loop. This applies for endpoint functions and background tasks you create, but also
-for internal Starlette code.
+Starlette uses a thread pool in several scenarios to avoid blocking the event loop:
+
+- When you create a synchronous endpoint using `def` instead of `async def`
+- When serving files with [`FileResponse`](responses.md#fileresponse)
+- When handling file uploads with [`UploadFile`](requests.md#request-files)
+- When running synchronous background tasks with [`BackgroundTask`](background.md)
+- And some other scenarios that may not be documented...
+
+Starlette will run your code in a thread pool to avoid blocking the event loop.
+This applies for endpoint functions and background tasks you create, but also for internal Starlette code.
 
 To be more precise, Starlette uses `anyio.to_thread.run_sync` to run the synchronous code.
 
-## Limitation
+## Concurrency Limitations
 
 The default thread pool size is only 40 _tokens_. This means that only 40 threads can run at the same time.
+This limit is shared with other libraries: for example FastAPI also uses `anyio` to run sync dependencies, which also uses up thread capacity.
 
 If you need to run more threads, you can increase the number of _tokens_: