1

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 :)

brandon
  • 31
  • 4
  • Please havea look at [this answer](https://stackoverflow.com/a/70873984/17865804) and [this answer](https://stackoverflow.com/a/71517830/17865804) – Chris Apr 25 '23 at 04:44

0 Answers0