Python FastAPI: как запустить поток в фоновом режиме? ⇐ Python
Python FastAPI: как запустить поток в фоновом режиме?
I'm making a server in python using FastAPI, and I want a function that is not related to my API, to run in background every 5 minutes (like checking stuff from an API and printing stuff depending on the response)
I've tried to make a thread that runs the function start_worker, but it doesn't print anything.
Does anyone know how to do so ?
def start_worker(): print('[main]: starting worker...') my_worker = worker.Worker() my_worker.working_loop() # this function prints "hello" every 5 seconds if __name__ == '__main__': print('[main]: starting...') uvicorn.run(app, host="0.0.0.0", port=8000, reload=True) _worker_thread = Thread(target=start_worker, daemon=False) _worker_thread.start()
Источник: https://stackoverflow.com/questions/708 ... background
I'm making a server in python using FastAPI, and I want a function that is not related to my API, to run in background every 5 minutes (like checking stuff from an API and printing stuff depending on the response)
I've tried to make a thread that runs the function start_worker, but it doesn't print anything.
Does anyone know how to do so ?
def start_worker(): print('[main]: starting worker...') my_worker = worker.Worker() my_worker.working_loop() # this function prints "hello" every 5 seconds if __name__ == '__main__': print('[main]: starting...') uvicorn.run(app, host="0.0.0.0", port=8000, reload=True) _worker_thread = Thread(target=start_worker, daemon=False) _worker_thread.start()
Источник: https://stackoverflow.com/questions/708 ... background
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение