Skip to content

Instantly share code, notes, and snippets.

@Juul
Created October 28, 2024 08:48
Show Gist options
  • Save Juul/c6a7ef5750597a7fcfb6d543452ecb1f to your computer and use it in GitHub Desktop.
Save Juul/c6a7ef5750597a7fcfb6d543452ecb1f to your computer and use it in GitHub Desktop.
python asyncio pipe stdin from one subprocess to another

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())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment