0

I am trying to call asyncio.create_subprocess_exec within a Fastapi background task, but it keeps raising a NotImplementedError. The run_subprocess function works fine when it is ran outside of Fastapi. I am running this in windows using an asyncio loop, not uvloop.

import asyncio
from fastapi import FastAPI, BackgroundTasks

DHCP_SERVER = "1.1.1.1"

app = FastAPI()

@app.get("/")
async def subprocess_test(background_tasks: BackgroundTasks):
  background_tasks.add_task(run_subprocess)

async def run_subprocess():
  proc = await asyncio.create_subprocess_exec(
    'powershell.exe',
    f'Get-Dhcp-Serverv4Scope -ComputerName \"{DHCP_SERVER}\"',
    stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE
  )

  stdout, stderr = await proc.communicate()

  if stderr:
    print(stderr)
  else:
    print(stdout)


File ".\subprocess_example.py", line 13 in run_subprocess
  proc = await asyncio.create_subprocess_exec(
File "C:\Python\Python38-32\lib\asyncio\subprocess.py", line 236, in create_subprcess_exec
  transport, protocol = await loop.subprocess_exec(
File "C:\Python\Python38-32\lib\asyncio\base_events.py", line 1615, in subprocess_exec
  transport = await self._make_subprocess_transport(
File "C:\Python\Python38-32\lib\asyncio\base_events.py", line 487, in _make_subprocess_transport
  raise NotImplementedError

Can anyone please help fix this?

Thanks!

Bshaw
  • 43
  • 7
  • What are you using for your event loop? FastAPI doesn't ship with its own, and the problem you're seeing is due to using an event loop that inherits from `asyncio`'s `BaseEventLoop` without providing an implementation of `_make_subprocess_transport` (the concrete classes `ProactorEventLoop` and `SelectorEventLoop` both define it, and they're the default on Windows and UNIX-likes respectively). What are you using, and how are you using it (basically, can you provide a [MCVE])? – ShadowRanger Feb 05 '20 at 21:34
  • I am running this in windows using an asyncio event loop and not uvloop. I have updated the code above to provide a minimal reproducible example. – Bshaw Feb 06 '20 at 13:48
  • Hey, OP, did you mange to figure it out? – Curtwagner1984 Jan 03 '22 at 15:06

1 Answers1

0

I believe this error is triggered because fastapi uses uvloop and asyncio doesn't know about this without setting a policy, there's an answer that provides some hooks how to achieve this;

asyncio event loop equivalent with uvloop

Hedde van der Heide
  • 21,841
  • 13
  • 71
  • 100