Put this in cmd1.sh
#!/bin/bash
while true; do
echo "hello"
echo "you"
sleep 1
done
Put this in cmd2.sh
:
#!/bin/bash
grep --line-buffered "h"
#!/usr/bin/env python3
import asyncio
import os
async def read_stream(stream):
while True:
line = await stream.readline()
if not line:
break
line = line.decode('utf-8').rstrip()
print(f"Subprocess output: {line}")
async def test():
r, w = os.pipe()
p1 = await asyncio.create_subprocess_shell("./cmd1.sh",
bufsize=0,
stdout=w)
os.close(w)
p2 = await asyncio.create_subprocess_shell("./cmd2.sh",
bufsize=0,
stdin=r,
stdout=asyncio.subprocess.PIPE)
os.close(r)
asyncio.create_task(read_stream(p2.stdout))
await p1.wait()
asyncio.run(test())