Can not run a subprocess in a test: "the child watcher does not have a loop attached" #2058
Closed
Description
Long story short
The loop created for the testing purpose doesn't allow you to spawn subprocesses using asyncio.create_subprocess_*.
Expected behaviour
You can spawn subprocesses without problem like you would do with a default loop.
Actual behaviour
When you try to spawn a subprocess, you automatically get:
self = <asyncio.unix_events.SafeChildWatcher object at 0x7fe80eb15e10>, pid = 9399
callback = <bound method _UnixSelectorEventLoop._child_watcher_callback of <_UnixSelectorEventLoop running=False closed=False debug=False>>
args = (<_UnixSubprocessTransport pid=9399 running>,)
def add_child_handler(self, pid, callback, *args):
if self._loop is None:
raise RuntimeError(
> "Cannot add child handler, "
"the child watcher does not have a loop attached")
E RuntimeError: Cannot add child handler, the child watcher does not have a loop attached
/usr/lib64/python3.6/asyncio/unix_events.py:858: RuntimeError
Steps to reproduce
import asyncio
# tokio event loop does not allow to override attributes
def skip_if_no_dict(loop):
if not hasattr(loop, '__dict__'):
pytest.skip("can not override loop attributes")
def stopper(loop):
def f(*args):
loop.call_later(0.001, loop.stop)
return f
async def test_subprocess_co(loop):
proc = await asyncio.create_subprocess_shell("exit 0", loop=loop)
await proc.wait()
assert proc.retcode == 0
def test_subprocess(loop, mocker):
skip_if_no_dict(loop)
loop.run_until_complete(test_subprocess_co(loop))Your environment
Arch Linux, Python 3.6