I have a process that reads input and writes output, like this doubler:
(in reality it's actually a black box and the input and the output are completely independent)
#!/bin/bash
while read -r i; do
sleep 0.$RANDOM
echo $((i*2))
done
and a few functions in my Python code that feeds this process asynchronously:
import asyncio
import subprocess
import random
class Feeder:
def __init__(self):
self.process = subprocess.Popen(['doubler.sh'],
stdin=subprocess.PIPE)
def feed(self, value):
self.process.stdin.write(str(value).encode() + b'\n')
self.process.stdin.flush()
feeder = Feeder()
async def feed_random():
while True:
feeder.feed(random.randint(0, 100))
await asyncio.sleep(1)
async def feed_tens():
while True:
feeder.feed(10)
await asyncio.sleep(3.14)
async def main():
await asyncio.gather(
feed_random(),
feed_tens(),
)
if __name__ == '__main__':
asyncio.run(main())
This works well. But I would like to read the output of the process too, like this:
...
stdout=subprocess.PIPE
...
for line in feeder.process.stdout:
print("The answer is " + line.decode())
but that is blocking, so the feeding won't happen. Can this be done in the same asyncio loop? Or do I need another thread?
process.stdout