Skip to content

Instantly share code, notes, and snippets.

@h2oota
Created August 10, 2021 01:40
Show Gist options
  • Save h2oota/a178655c49ad46413e90029c26600c61 to your computer and use it in GitHub Desktop.
Save h2oota/a178655c49ad46413e90029c26600c61 to your computer and use it in GitHub Desktop.
async def create_piped_processes(
*cmds, stdin = asyncio.subprocess.DEVNULL, stdout = None):
if stdout is None:
out, w = os.pipe()
else:
out = None
w = stdout
p = []
for ii, c in reversed(tuple(enumerate(cmds))):
if ii == 0:
i = stdin
else:
i, o = os.pipe()
p.append(await asyncio.subprocess.create_subprocess_exec(
*c, stdout = w, stdin = i))
os.close(w)
if ii > 0:
os.close(i)
w = o
return out, map(lambda p: p.wait(), p)
'''
Usage:
fd, procs = await create_piped_processes(
['xzcat', out.name],
['restore', '-t', '-f', '-'])
for line in os.read(fd, 1024).decode('utf-8').splitlines():
...
os.close(fd)
status = await asyncio.gather(*procs)
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment