FastAPI python: How to run a thread in the background?
Option 1 You should start your Thread before calling uvicorn.run, as uvicorn.run is blocking the thread. import time import threading from fastapi import FastAPI import uvicorn app = FastAPI() class BackgroundTasks(threading.Thread): def run(self,*args,**kwargs): while True: print(‘Hello’) time.sleep(5) if __name__ == ‘__main__’: t = BackgroundTasks() t.start() uvicorn.run(app, host=”0.0.0.0″, port=8000) You could also start your thread using … Read more