Performance with background tasks

Aperta
#396 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

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

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

brainstorming

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 --workers flag 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.futures or multiprocessing for 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

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di pepkit/pephub

Tutte le issue di pepkit/pephub

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.