environment: Windows10 Python 3.8.2 Fastapi 0.63.0 uvicorn 0.13.2
app.py
import asyncio
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
async def test():
proc = await asyncio.create_subprocess_shell("echo 100")
await proc.communicate()
If you create app.py as above, start it, and then access http://127.0.0.1:8000, a NotImplementedError will occur and echo 100 will not be executed.
Because I use SelectorEventLoop As you can see in Official Document, it seems that subprocess cannot be used with SelectorEventLoop.
Issue was growing on GitHub It's a good idea to change the event loop used by uvicorn to ProactorEventLoop. In particular
The guy who replaces the event loop
from uvicorn import Config, Server
from asyncio import ProactorEventLoop, set_event_loop, ProactorEventLoop, get_event_loop
if __name__ == "__main__":
set_event_loop(ProactorEventLoop())
server = Server(config=Config(app=app))
get_event_loop().run_until_complete(server.serve())
Quoted from (https://github.com/tiangolo/fastapi/issues/1110#issuecomment-599506004)
Recommended Posts