import asyncio
from sanic import Sanic
from sanic.response import empty
app = Sanic(__name__)
@app.get("/")
async def unused(request):
return empty()
@app.before_server_start
async def run(*ignored):
proc = await asyncio.subprocess.create_subprocess_exec("echo", "foo")
await proc.wait()
if __name__ == "__main__":
asyncio.run(run())
If the above is run as script, it prints “foo”, as is expected (but exits without running Sanic). When ran with Sanic CLI (or if code is modified to use app.run), I get
File "mycode.py", line 13, in run
proc = await asyncio.subprocess.create_subprocess_exec("echo", "foo")
File "C:\Python311\Lib\asyncio\subprocess.py", line 218, in create_subprocess_exec
transport, protocol = await loop.subprocess_exec(
File "C:\Python311\Lib\asyncio\base_events.py", line 1680, in subprocess_exec
transport = await self._make_subprocess_transport(
File "C:\Python311\Lib\asyncio\base_events.py", line 502, in _make_subprocess_transport
raise NotImplementedError
NotImplementedError
Apparently Sanic does something with asyncio loop/internals to make asyncio.subprocess NotImplemented. Running the same code on Linux works fine. I used before_server_start but the same happens anywhere within a Sanic app (handlers etc). The same also on Python 3.10 (instead of 3.11 used here).