2

I'm trying to run a Subprocess in a FastApi route, but the execution results in NotImplementedError. I've read similar questions on the issue:

Why am I getting NotImplementedError with async and await on Windows?

Asyncio.create_subprocess_exec NotImplementedError - Fastapi Background Task

But it doesn't seem they have any viable solutions.

My FastApi route looks like this:

@app.get("/test/subprocess")
async def subprocess_test():
    parsed_json = await(probe_video_file(Path(r"G:\ffmpeg testing\ffmpeg\ffprobe.exe"),
                                         Path(r"G:\ffmpeg testing\input_file\test_file.wmv")))
    print(parsed_json)
    return parsed_json

When I navigate to this rout, an exception is raised and the code crushes. The probe_video_file function has a subprocess call inside it, like so:

async def probe_video_file(ffprobe_path: Path, file_to_probe: Path) -> dict:
    """
    Probes a video file with FFprobe.
    :param ffprobe_path: Path to ffprobe executable
    :param file_to_probe: Path to file to probe
    :returns Parsed JSON dict of the output
    """
    args = [f'{str(ffprobe_path)}', f'{str(file_to_probe)}']
    args += ["-hide_banner", "-loglevel", "fatal", "-show_error", "-show_format", "-show_streams", "-show_chapters",
             "-show_private_data", "-print_format", "json"]

    #sub process is here, and here is where the exception happens.
    proc = await asyncio.create_subprocess_exec(
        *args,
        stdout=asyncio.subprocess.PIPE,
        stderr=asyncio.subprocess.PIPE)

    stdout, stderr = await proc.communicate()

    print(f'exited with {proc.returncode}]')
    if stdout:
        print(f'[stdout]\n{stdout.decode()}')
    if stderr:
        print(f'[stderr]\n{stderr.decode()}')
        raise Exception(f"Failed to probe file {file_to_probe}")

    return json.loads(stdout)

When I run probe_video_file without FastAPI, like so:

async def main():
    json_output = await probe_video_file(Path(r"G:\ffmpeg testing\ffmpeg\ffprobe.exe"),
                                         Path(r"G:\ffmpeg testing\input_file\test_file.wmv"))
    print(json_output)


if __name__ == '__main__':
    asyncio.run(main())

It runs just fine and prints the correct output:

[['G:\\ffmpeg testing\\ffmpeg\\ffprobe.exe', 'G:\\ffmpeg testing\\input_file\\test_file.wmv', '-hide_banner', '-loglevel', 'fatal', '-show_error', '-show_format', '-show_streams', '-show_chapters', '-show_private_data', '-print_format', 'json'] exited with 0]
[stdout]
{
    "streams": [
        {
            "index": 0,
            "codec_name": "wmav2",
            "codec_long_name": "Windows Media Audio 2",
            "codec_type": "audio",
            "codec_tag_string": "a[1][0][0]",
            "codec_tag": "0x0161",
            "sample_fmt": "fltp",
            "sample_rate": "48000",
            "channels": 2,
            "bits_per_sample": 0,
            "r_frame_rate": "0/0",
            "avg_frame_rate": "0/0",
            "time_base": "1/1000",
            "start_pts": 0,
            "start_time": "0.000000",
            "duration_ts": 2155050,
            "duration": "2155.050000",
            "bit_rate": "96000",
            "disposition": {
                "default": 0,
                "dub": 0,
                "original": 0,
                "comment": 0,
                "lyrics": 0,
                "karaoke": 0,
                "forced": 0,
                "hearing_impaired": 0,
                "visual_impaired": 0,
                "clean_effects": 0,
                "attached_pic": 0,
                "timed_thumbnails": 0
            },
            "tags": {
                "language": "eng"
            }
        },
        {
            "index": 1,
            "codec_name": "wmv3",
            "codec_long_name": "Windows Media Video 9",
            "profile": "Main",
            "codec_type": "video",
            "codec_tag_string": "WMV3",
            "codec_tag": "0x33564d57",
            "width": 850,
            "height": 480,
            "coded_width": 850,
            "coded_height": 480,
            "closed_captions": 0,
            "has_b_frames": 0,
            "pix_fmt": "yuv420p",
            "level": -99,
            "chroma_location": "left",
            "refs": 1,
            "r_frame_rate": "30000/1001",
            "avg_frame_rate": "30000/1001",
            "time_base": "1/1000",
            "start_pts": 0,
            "start_time": "0.000000",
            "duration_ts": 2155050,
            "duration": "2155.050000",
            "bit_rate": "2000000",
            "disposition": {
                "default": 0,
                "dub": 0,
                "original": 0,
                "comment": 0,
                "lyrics": 0,
                "karaoke": 0,
                "forced": 0,
                "hearing_impaired": 0,
                "visual_impaired": 0,
                "clean_effects": 0,
                "attached_pic": 0,
                "timed_thumbnails": 0
            },
            "tags": {
                "language": "eng"
            }
        }
    ],
    "chapters": [

    ],
    "format": {
        "filename": "G:\\ffmpeg testing\\input_file\\test_file.wmv",
        "nb_streams": 2,
        "nb_programs": 0,
        "format_name": "asf",
        "format_long_name": "ASF (Advanced / Active Streaming Format)",
        "start_time": "0.000000",
        "duration": "2155.050000",
        "size": "567194391",
        "bit_rate": "2105545",
        "probe_score": 100,
        "tags": {
            "WMFSDKNeeded": "0.0.0.0000",
            "DeviceConformanceTemplate": "MP@HL",
            "WMFSDKVersion": "11.0.5721.5265",
            "IsVBR": "0"
        }
    }
}

{'streams': [{'index': 0, 'codec_name': 'wmav2', 'codec_long_name': 'Windows Media Audio 2', 'codec_type': 'audio', 'codec_tag_string': 'a[1][0][0]', 'codec_tag': '0x0161', 'sample_fmt': 'fltp', 'sample_rate': '48000', 'channels': 2, 'bits_per_sample': 0, 'r_frame_rate': '0/0', 'avg_frame_rate': '0/0', 'time_base': '1/1000', 'start_pts': 0, 'start_time': '0.000000', 'duration_ts': 2155050, 'duration': '2155.050000', 'bit_rate': '96000', 'disposition': {'default': 0, 'dub': 0, 'original': 0, 'comment': 0, 'lyrics': 0, 'karaoke': 0, 'forced': 0, 'hearing_impaired': 0, 'visual_impaired': 0, 'clean_effects': 0, 'attached_pic': 0, 'timed_thumbnails': 0}, 'tags': {'language': 'eng'}}, {'index': 1, 'codec_name': 'wmv3', 'codec_long_name': 'Windows Media Video 9', 'profile': 'Main', 'codec_type': 'video', 'codec_tag_string': 'WMV3', 'codec_tag': '0x33564d57', 'width': 850, 'height': 480, 'coded_width': 850, 'coded_height': 480, 'closed_captions': 0, 'has_b_frames': 0, 'pix_fmt': 'yuv420p', 'level': -99, 'chroma_location': 'left', 'refs': 1, 'r_frame_rate': '30000/1001', 'avg_frame_rate': '30000/1001', 'time_base': '1/1000', 'start_pts': 0, 'start_time': '0.000000', 'duration_ts': 2155050, 'duration': '2155.050000', 'bit_rate': '2000000', 'disposition': {'default': 0, 'dub': 0, 'original': 0, 'comment': 0, 'lyrics': 0, 'karaoke': 0, 'forced': 0, 'hearing_impaired': 0, 'visual_impaired': 0, 'clean_effects': 0, 'attached_pic': 0, 'timed_thumbnails': 0}, 'tags': {'language': 'eng'}}], 'chapters': [], 'format': {'filename': 'G:\\ffmpeg testing\\input_file\\test_file.wmv', 'nb_streams': 2, 'nb_programs': 0, 'format_name': 'asf', 'format_long_name': 'ASF (Advanced / Active Streaming Format)', 'start_time': '0.000000', 'duration': '2155.050000', 'size': '567194391', 'bit_rate': '2105545', 'probe_score': 100, 'tags': {'WMFSDKNeeded': '0.0.0.0000', 'DeviceConformanceTemplate': 'MP@HL', 'WMFSDKVersion': '11.0.5721.5265', 'IsVBR': '0'}}}

Process finished with exit code 0

I've tried 'setting' the event loop to ProactorEventLoop like it's suggested in the other questions:

@app.on_event("startup")
async def startup_event():
    """Code runs at startup..."""
    loop = asyncio.ProactorEventLoop()
    asyncio.set_event_loop(loop)

But it had no effect.

Here is the exception traceback:

ERROR:    Exception in ASGI application
Traceback (most recent call last):
  File "E:\pycharm\my_project\venv\lib\site-packages\uvicorn\protocols\http\httptools_impl.py", line 375, in run_asgi
    result = await app(self.scope, self.receive, self.send)
  File "E:\pycharm\my_project\venv\lib\site-packages\uvicorn\middleware\proxy_headers.py", line 75, in __call__
    return await self.app(scope, receive, send)
  File "E:\pycharm\my_project\venv\lib\site-packages\fastapi\applications.py", line 208, in __call__
    await super().__call__(scope, receive, send)
  File "E:\pycharm\my_project\venv\lib\site-packages\starlette\applications.py", line 112, in __call__
    await self.middleware_stack(scope, receive, send)
  File "E:\pycharm\my_project\venv\lib\site-packages\starlette\middleware\errors.py", line 181, in __call__
    raise exc
  File "E:\pycharm\my_project\venv\lib\site-packages\starlette\middleware\errors.py", line 159, in __call__
    await self.app(scope, receive, _send)
  File "E:\pycharm\my_project\venv\lib\site-packages\starlette\middleware\cors.py", line 84, in __call__
    await self.app(scope, receive, send)
  File "E:\pycharm\my_project\venv\lib\site-packages\starlette\exceptions.py", line 82, in __call__
    raise exc
  File "E:\pycharm\my_project\venv\lib\site-packages\starlette\exceptions.py", line 71, in __call__
    await self.app(scope, receive, sender)
  File "E:\pycharm\my_project\venv\lib\site-packages\starlette\routing.py", line 656, in __call__
    await route.handle(scope, receive, send)
  File "E:\pycharm\my_project\venv\lib\site-packages\starlette\routing.py", line 259, in handle
    await self.app(scope, receive, send)
  File "E:\pycharm\my_project\venv\lib\site-packages\starlette\routing.py", line 61, in app
    response = await func(request)
  File "E:\pycharm\my_project\venv\lib\site-packages\fastapi\routing.py", line 226, in app
    raw_response = await run_endpoint_function(
  File "E:\pycharm\my_project\venv\lib\site-packages\fastapi\routing.py", line 159, in run_endpoint_function
    return await dependant.call(**values)
  File "E:\pycharm\my_project\src\backend\app\api\api.py", line 202, in subprocess_test
    parsed_json = await(probe_video_file(Path(r"G:\ffmpeg testing\ffmpeg\ffprobe.exe"),
  File "E:\pycharm\my_project\src\backend\app\ffmpeg\ffprobe.py", line 26, in probe_video_file
    proc = await asyncio.create_subprocess_exec(
  File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\asyncio\subprocess.py", line 218, in create_subprocess_exec
    transport, protocol = await loop.subprocess_exec(
  File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 1652, in subprocess_exec
    transport = await self._make_subprocess_transport(
  File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 493, in _make_subprocess_transport
    raise NotImplementedError
NotImplementedError
INFO:     127.0.0.1:58576 - "GET /test/subprocess HTTP/1.1" 500 Internal Server Error

Does anyone know how to resolve it? From what I understand this has to do with FastApi's default event loop not supporting this. But I don't know how to set or replace the default FastApi event loop with Asyncio's default event loop.

EDIT:

I think it's important to point out that I'm using windows 10.

Curtwagner1984
  • 1,908
  • 4
  • 30
  • 48
  • https://github.com/tiangolo/fastapi/issues/825#issuecomment-569826743 – gold_cy Jan 03 '22 at 15:49
  • @gold_cy Could you elaborate on that? I'm not sure what to do with that? Where does it go? It should be in FastAPI configuration or Uvicron configuration? A bit more clarity will help. – Curtwagner1984 Jan 03 '22 at 16:06
  • @gold_cy I've added it before `uvicorn.run("api.api:app", host="0.0.0.0", port=8000, reload=True)` in the main file. However `loop` keyword argument expects a string literal and not a loop instance and it errors out if I give it an instance. It expects one of the following: `["none","auto","asyncio","uvloop"]` – Curtwagner1984 Jan 03 '22 at 16:16
  • that’s my point I don’t think you can use any other loop other than the ones provided – gold_cy Jan 03 '22 at 17:03
  • Which python version are you using? With python 3.10 (maybe it started with an earlier version), `ProactorEventLoop` is the default event loop for Windows. Otherwise it's possible to use a custom event loop with uvicorn, e.g. by subclassing `uvicorn.Server`. I did this before it became the default and can look up the details if you need to do it. – thisisalsomypassword Jan 03 '22 at 17:13
  • @thisisalsomypassword I'm on 3.10 Python version – Curtwagner1984 Jan 03 '22 at 18:03
  • @gold_cy So it's impossible to run an async subprocess on FastApi? – Curtwagner1984 Jan 03 '22 at 18:12
  • Did you check which event loop is really running? What output do you get for `print(asyncio.get_running_loop())` inside your fastapi route function `subprocess_test()`? – thisisalsomypassword Jan 03 '22 at 18:21
  • @thisisalsomypassword It says `<_WindowsSelectorEventLoop running=True closed=False debug=False>` It says this even if I say ` loop = asyncio.ProactorEventLoop()` and `asyncio.set_event_loop(loop)` before printing. – Curtwagner1984 Jan 03 '22 at 18:36

1 Answers1

5

Looking at the uvicorn code a bit closer, it seems that the reload=True options causes the default ProactorEventLoop to be changed to SelectorEventLoop on windows. You can try it without reload=True and see if it works (I can't test it right now). But if you need to enforce the use of a specific event loop in uvicorn, you can subclass uvicorn.Server like so:

import asyncio
from asyncio.windows_events import ProactorEventLoop

from fastapi import FastAPI
from uvicorn import Config, Server

app = FastAPI()


class ProactorServer(Server):
    def run(self, sockets=None):
        loop = ProactorEventLoop()
        asyncio.set_event_loop(loop) # since this is the default in Python 3.10, explicit selection can also be omitted
        asyncio.run(self.serve(sockets=sockets))


config = Config(app=app, host="0.0.0.0", port=8000, reload=True)
server = ProactorServer(config=config)
server.run()

This should allow you to use asyncio.create_subprocess_exec() within your FastAPI endpoint.

thisisalsomypassword
  • 1,423
  • 1
  • 6
  • 17
  • Thank you so much! Subclassing `Server` definitely worked, but omitting `reload=True` from the original code did not. Are there any downsides to using the `ProactorEventLoop` and this is why unicorn don't use it? – Curtwagner1984 Jan 03 '22 at 19:14
  • Not that I know of. I'm using the same setup in a project and didn't have any issues yet. I saw you postet an issue on the uvicorn github. Maybe that's the place to ask why they are doing it that way. – thisisalsomypassword Jan 03 '22 at 19:46
  • Sadly enough I posted an issue on the FastApi Github, not the Uvicron one :( – Curtwagner1984 Jan 03 '22 at 20:11
  • 1
    Thanks man, stuck in this for a considerate time, I just removed --reload – Edi Jan 23 '23 at 10:45