This question is similar to my other recent question here ( python: fork without an external command, and capturing stdout and stderr separately ), except this question relates to the asyncio
world.
The asyncio.create_subprocess_{shell,exec}
functions provide a convenient way to run an executable in a subprocess and to interact with its stdin
, stdout
, and stderr
via asyncio-enabled pipes. I'm looking for a similar procedure that will run an arbitrary function in a subprocess and to provide the same asyncio-enabled pipes to its stdin
, stdout
, and stderr
. For example, suppose this hypothetical function is called asyncio.create_subprocess_lambda
. I would want it to work more or less as follows:
def myfunc(*args, **kwargs):
# does arbitrary things with stdin, stdout, and stderr.
return 0
async def run():
proc = await asyncio.create_subprocess_lambda(
lambda : myfunc('a', 'b', c='d'),
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE)
stdout, stderr = await proc.communicate()
print(f'[lambda exited with {proc.returncode}]')
if stdout:
print(f'[stdout]\n{stdout.decode()}')
if stderr:
print(f'[stderr]\n{stderr.decode()}')
This would cause a fork to take place with the myfunc
function running as a child, and with its stdout
and stderr
output accessible in the parent via asynchronous pipes.
I know that such a function doesn't currently exist, but I'm hoping that some other asyncio
methodology is available that would allow me to implement this functionality with a minimum of complex, convoluted code.
Thank you in advance for any suggestions.