0

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.

HippoMan
  • 2,119
  • 2
  • 25
  • 48
  • Is operating on stdin/stdout/stderr the only way you can communicate with the parent? Would it work for you if you could send objects through a shared queue? – user4815162342 Nov 23 '19 at 10:32
  • The child is a function which uses 3rd-party packages, some of which write to stderr and stdout. Unless I rewrite all of these subsidiary packages, I'm stuck with having to capture stdout and stderr. – HippoMan Nov 25 '19 at 13:21

0 Answers0