Код: Выделить всё
import time
import asyncio
from fastapi import FastAPI, status
app = FastAPI()
fifo_queue = asyncio.Queue()
async def process_requests():
while True:
name = await fifo_queue.get() # Wait for a request from the queue
print(name)
time.sleep(10) # A RESOURCE INTENSIVE JOB THAT BLOCKS THE THREAD
fifo_queue.task_done() # Indicate that the request has been processed
@app.on_event("startup")
async def startup_event():
asyncio.create_task(process_requests()) # Start the request processing task
@app.get("/render")
async def render(name):
fifo_queue.put_nowait(name) # Add the request parameter to the queue
return status.HTTP_201_CREATED # Return a 201 status code
< /code>
Проблема с этим подходом заключается в том, что сервер не остается отзывчивым. После отправки первого запроса он занят полный рабочий день и не отвечает, как я надеялся. < /P>
curl http://127.0.0.1:8000/render\?name\=001
Есть идеи?
Подробнее здесь: https://stackoverflow.com/questions/778 ... responsive