Performance with background tasks
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 25/100
- Tipo di issue
- Documentazione
- Chiarezza
- Da chiarire
- Stato di attività
- Ferma
- Stack tecnologico
- fastapi, python
- Ambito
- api, backend, performance
Direzione di ricerca
Inizia dalla pagina collegata di FastAPI relativa a deployment/server-workers e individua l’entry point del server del repository; nell’issue non sono indicati file o test. Il lavoro sarebbe completato con una raccomandazione documentata e specifica del repository per mantenere performante il server durante le attività in background intensive per la CPU, insieme a un modo per convalidare la raccomandazione.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
How can we make sure the server stays performant even if a task is requiring some CPU?
https://fastapi.tiangolo.com/deployment/server-workers/
Here's an answer from ChatGPT:
FastAPI can handle asynchronous requests even when running on a single core. The key here is that FastAPI is designed to be asynchronous, meaning that even on a single core, it can interleave tasks using Python's asyncio framework. This allows the server to handle multiple requests concurrently, as long as they aren't CPU-bound.
Here's how this works:
- Non-blocking operations: When one request is waiting for an I/O operation to complete (like a database call or external API request), the server can switch to handling another request.
- Concurrency, not parallelism: With one core, FastAPI can perform asynchronous tasks concurrently but not in parallel. So, the benefit of async is mainly realized when requests involve waiting (like I/O).
Example scenario on a single core:
- Request 1 comes in and initiates a long-running I/O operation (e.g., fetching data from a database).
- Instead of waiting for the operation to complete, the server can handle Request 2 while Request 1 is still "waiting."
- Once Request 1’s operation is done, the server can continue processing it.
CPU-bound tasks on a single core:
If your tasks are CPU-bound (like intensive data processing), using async alone won’t improve performance, and the server will process tasks sequentially. For heavy CPU tasks, you'd need multiple processes or threads.
Multiple cores:
To take advantage of multiple cores, you can use a process manager like Uvicorn with workers:
- Uvicorn with the
--workersflag starts multiple worker processes, each running a FastAPI app instance on its own core. - For true parallelism across cores, you can start multiple worker processes with Uvicorn or use Python’s
concurrent.futuresormultiprocessingfor CPU-bound parallel tasks.
You don’t need to do anything special in your Python code to take advantage of multiple cores, but you do need to configure Uvicorn or similar to use more workers for CPU-bound operations.
In summary:
- FastAPI can handle asynchronous requests on a single core, thanks to
asyncio. - To fully utilize multiple cores for CPU-bound tasks, you need to configure Uvicorn with multiple workers.
- Lingua principale
- TypeScript
- Stelle
- 22
- Fork
- 2
- Merge medio
- 6g 20h
- PR unite (30g)
- 2
Preparare l'ambiente
- Include un Dockerfile o un file Docker Compose
- Ha un modello di pull request
- Nessuna guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di pepkit/pephub
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 64/100
-
enhancement
Difficoltà 4/5 3-5 giorni Idoneità per principianti 48/100
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 35/100
-
Better authenticationAperta
Difficoltà 5/5 Più di una settimana Idoneità per principianti 35/100
-
Ordering PEP csv by colunnAperta
Difficoltà 3/5 1-2 giorni Idoneità per principianti 35/100
Tutte le issue di pepkit/pephub
Issue simili
-
Translate the appApertagood first issue help wanted
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 92/100
Mikiyas-Meles-Assefa/opensourcebook#154 ·
I maintainer di solito rispondono entro 1 giorno
-
usersim
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
WenyuChiou/agent-board#2315 ·
I maintainer di solito rispondono entro 1 giorno
-
bug data-types
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100
ClickHouse/clickhouse-js#1293 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
sidebandstudio/clawtrate#1863 ·
I maintainer di solito rispondono entro 1 giorno
-
area: middleware enhancement good first issue
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100