[PYTHON] [Windows] Cause and solution of NotImplementedError when using asyncio.create_subprocess_shell () with Fast API

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.

Cause

Because I use SelectorEventLoop As you can see in Official Document, it seems that subprocess cannot be used with SelectorEventLoop.

Solutions

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

[Windows] Cause and solution of NotImplementedError when using asyncio.create_subprocess_shell () with Fast API
[Python] Error and solution memo when using venv with pyenv + anaconda
Various memorandums when using sdk of LINE Messaging API with Python (2.7.9) + Google App Engine
Get data using Ministry of Internal Affairs and Communications API
Using Docker (Hyper-V) with PyCharm on Windows 10 (as of August 2017)
Using MLflow with Databricks ② --Visualization of experimental parameters and metrics -
When using optparse with iPython
When using if and when using while
Use configparser when using API
PATH when using ANACONDA virtual environment with Pycharm (as of Mac 2020/10/03)
Implementation of CRUD using REST API with Python + Django Rest framework + igGrid
# Solution when pip install gives an error when using Anaconda on Windows 10
Error and solution when installing python3 with homebrew on mac (catalina 10.15)
Correspondence analysis of sentences with COTOHA API and save to file
Crawling with Python and Twitter API 2-Implementation of user search function
python> No #ifdef> Another solution> __debug__ Pros and cons of using
Firebase Authentication token issuance in Python and token verification with Fast API
[Ruby on Rails] Display and pinning of GoolgeMAP using Google API