2

While trying to develop a FastAPI client in Spyder environment, one cannot start the execution.

The code fails and raises:

RuntimeError: asyncio.run() cannot be called from a running event loop Minimum code to be executed:

from fastapi import FastAPI
import uvicorn

app = FastAPI()


@app.get("/")
async def root():
    return {"message": "Hello World"}


if __name__ == '__main__':
    uvicorn.run(app, port=8080, host='0.0.0.0')
  • Found a solution in the following post: https://stackoverflow.com/questions/50243393/runtimeerror-this-event-loop-is-already-running-debugging-aiohttp-asyncio-a . – Micha Lemarenko Nov 26 '21 at 01:56

1 Answers1

1

(Spyder maintainer here) If you want to run applications that use asyncio in Spyder, you need to install a package called nest_asyncio first and then run the following code in the IPython console before running your code:

import nest_asyncio
nest_asyncio.apply()

Note: You only need to run that code once.

Carlos Cordoba
  • 33,273
  • 10
  • 95
  • 124