-
-
Notifications
You must be signed in to change notification settings - Fork 8.5k
Closed
Labels
Description
How can I add permanently running background tasks?
I want to have the following behaviour:
- Start the api and
mainstarts to run as well. - Open the browser and call the endpoint
/. - It returns the current value of
value.
Example
import time
import uvicorn
from fastapi import FastAPI
app = FastAPI()
def main():
# where and how should i start this function
value = 0
while True:
time.sleep(0.1)
value += 1
@app.get("/")
def root():
# how can i read value from main?
return value
if __name__ == "__main__":
uvicorn.run("api:app", host="0.0.0.0", port=8001, reload=True)jthetzel, yannickpschroeder, smheidrich, EvgeniiaVak, artemonsh and 4 more