import logging
import threading
import time
import uvicorn
from fastapi import FastAPI
from sse_starlette import EventSourceResponse
app = FastAPI()
@app.get("/")
def stream_output():
logging.warning(f"{threading.current_thread().ident}")
return EventSourceResponse(num_generator(10), headers={"thread": str(threading.current_thread().ident)})
def num_generator(n):
for i in range(n):
logging.warning(f"{threading.currentThread().ident}: %s" % i)
time.sleep(2)
yield f"thread: {threading.current_thread().ident} num: {i}"
logging.warning(f"{threading.current_thread().ident}: end")
if __name__ == "__main__":
uvicorn.run(app, host='localhost', port=8000)
Результат испытания под давлением:

результат одного из запросов:
data: thread: 39404 num: 0
data: thread: 39404 num: 1
data: thread: 57624 num: 2
data: thread: 39404 num: 3
data: thread: 52536 num: 4
data: thread: 39404 num: 5
data: thread: 52536 num: 6
data: thread: 39404 num: 7
data: thread: 39404 num: 8
data: thread: 52536 num: 9
Я ожидаю того же потока для того же запроса
data: thread: 56052 num: 0
data: thread: 56052 num: 1
data: thread: 56052 num: 2
data: thread: 56052 num: 3
data: thread: 56052 num: 4
data: thread: 56052 num: 5
data: thread: 56052 num: 6
data: thread: 56052 num: 7
data: thread: 56052 num: 8
data: thread: 56052 num: 9
Подробнее здесь: https://stackoverflow.com/questions/788 ... -generator