【问题标题】:Cannot execute FastAPI client code in Spyder无法在 Spyder 中执行 FastAPI 客户端代码
【发布时间】:2021-11-26 01:52:31
【问题描述】:

尝试在 Spyder 环境中开发 FastAPI 客户端时,无法启动执行。

代码失败并引发:

RuntimeError: asyncio.run() 不能从正在运行的事件循环中调用 最少要执行的代码:

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

【问题讨论】:

标签: python python-asyncio spyder fastapi uvicorn


【解决方案1】:

(这里是 Spyder 维护者) 如果要在 Spyder 中运行使用asyncio 的应用程序,需要先安装一个名为nest_asyncio 的包,然后在 IPython 控制台中运行以下代码在运行代码之前:

import nest_asyncio
nest_asyncio.apply()

注意:您只需运行该代码一次。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-07
    • 2020-08-03
    • 1970-01-01
    • 2017-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-17
    相关资源
    最近更新 更多