Skip to main content

All Questions

102 votes
2 answers
84k views

FastAPI runs API calls in serial instead of parallel fashion

I have the following code: from fastapi import FastAPI, Request import time app = FastAPI() @app.get("/ping") async def ping(request: Request): print("Hello") time....
Learning from masters's user avatar
72 votes
4 answers
87k views

FastAPI asynchronous background tasks blocks other requests? [duplicate]

I want to run a simple background task in FastAPI, which involves some computation before dumping it into the database. However, the computation would block it from receiving any more requests. from ...
Gary Ong's user avatar
  • 1,048
54 votes
6 answers
41k views

Sharing python objects across multiple workers

We have created a service using FastAPI. When our service starts it creates a few python objects that the endpoints then use to store or retrieve data from. FastAPI in production starts with multiple ...
Dubraven's user avatar
  • 1,091
26 votes
1 answer
24k views

asyncpg - cannot perform operation: another operation is in progress

I am attempting to resolve the following error: asyncpg.exceptions._base.InterfaceError: cannot perform operation: another operation is in progress Here is the full traceback: Traceback (most recent ...
jclark754's user avatar
  • 1,014
12 votes
2 answers
19k views

What does async actually do in FastAPI? [duplicate]

I have two scripts: from fastapi import FastAPI import asyncio app = FastAPI() @app.get("/") async def root(): a = await asyncio.sleep(10) return {'Hello': 'World',} And second ...
filtertips's user avatar
9 votes
1 answer
6k views

Fastapi testing RuntimeError: Task attached to a different loop

I trying to test my endpoint with pytest main.py: from fastapi import FastAPI, status, HTTPException, Depends from sqlalchemy.ext.asyncio import AsyncSession from .schema import ClientIn, ClientOut, ...
KrySeyt's user avatar
  • 131
6 votes
1 answer
3k views

Will run_in_executor ever block?

suppose if I have a web server like this: from fastapi import FastAPI import uvicorn import asyncio app = FastAPI() def blocking_function(): import time time.sleep(5) return 42 @app.get(...
Bob Fang's user avatar
  • 7,441
5 votes
1 answer
5k views

Difference between await Coroutine and await Task

On FastAPI, I have an endpoint that calls get_1 or get_2 coroutine function below. get_1 uses await redis.get(key) get_2 uses await asyncio.ensure_future(redis.get(key)) Is there any difference ...
Rocherlee's user avatar
  • 2,801
5 votes
2 answers
4k views

Call async function from sync inside async FastAPI application

Not sure, that this is possible, but still I have default async FastAPI application, where I have sync function @classmethod def make_values( cls, records: list, symbol: str = ...
Headmaster's user avatar
  • 2,342
4 votes
2 answers
3k views

Python: Call asynchronous code from synchronous method when there is already an event loop running [duplicate]

I am working with FastAPI and uvloop to serve a REST API in an efficient way. I have a lot of asynchronous code that make calls to remote resources such as a database, a storage, etc, those functions ...
Nicolas Martinez's user avatar
3 votes
1 answer
1k views

FastAPI async routes deadlock bug when using infinite loop [duplicate]

I am experiencing an issue with FastAPI where the application gets deadlocked when using an infinite loop in an async route. Here is a simplified version of my FastAPI application (fastapi_test.py): ...
tombreplacer's user avatar
3 votes
1 answer
2k views

How to apply FastAPI Middleware on "non-async def" endpoints? [duplicate]

According to https://fastapi.tiangolo.com/tutorial/middleware/, we could apply a FastAPI Middleware on async def endpoints. Currently I have several non-async def endpoints, how to apply FastAPI ...
zhfkt's user avatar
  • 2,471
2 votes
2 answers
534 views

FastAPI TestClient not starting lifetime in test

Example code: import os import asyncio from contextlib import asynccontextmanager from fastapi import FastAPI, Request @asynccontextmanager async def lifespan(app: FastAPI): print(f'Lifetime ON {...
WebOrCode's user avatar
  • 7,322
1 vote
1 answer
205 views

AttributeError: 'function' object has no attribute 'submit' [duplicate]

I am using fastapi and uvicorn as my python server and whenever a user visits a route, I want to start a subprocess in the background. I used asyncio.create_task and loop.run_in_excutor in order to ...
Zaid's user avatar
  • 87
1 vote
1 answer
6k views

How do I call an async function in a new process using multiprocessing? [duplicate]

I created a server that wait for webhook signal, and when there is signal, it will create a new process to run the loop() function, and when running the loop() function, I want it to call the function ...
jguy's user avatar
  • 231

15 30 50 per page