Is it possible to do other things while hosting an API on a python script ?
from fastapi import FastAPI
import asyncio
app = FastAPI()
@app.get("/")
async def hello():
return {"Hello": "World"}
async def sometask():
while True:
print("executing...")
await asyncio.sleep(5)
loop = asyncio.get_event_loop()
loop.run_until_complete(sometask())
This I get the error RuntimeError: this event loop is already running.
EDIT : Here we can use asyncio.ensure_future(sometask())
and it works :)