简体   繁体   中英

Piping long-running processes

I'm fairly new to Linux/Python programming. I tried googling about this but could not find anything useful.

I wrote a simple script that reads lines from a serial port and prints them (as they are read) to stdout. Here's the relevant code:

ser = serial.Serial(args.port)
while True:
    print(ser.readline())

I also wrote a script (this is only for testing purposes) that echoes lines read from stdin to stdout. Here's the code for that:

while True:
   print(args.prefix + input())

I'm using python3, and the scripts are named serial.py and echo.py respectively.

What I would like to do is to pipe the output of serial to the input of echo (echo will later be replaced by a script that writes to a database), and leave those running indefinitely.

I tried both scripts separately and they work fine, but nothing gets printed when I pipe both commands:

./serial.py --port /dev/ttyACM0 | ./echo.py

It does work when I pipe echo to itself:

awer@napalm:~$ ./echo.py --prefix AAA | ./echo.py --prefix BBB
hi!
BBBAAAhi!

What am I doing wrong?

Thanks for any help on this.

Best regards

This could be an issue related to a buffered stdout. Try to run the serial.py using the '-u' flag of the python3 interpreter, which will force stdout and stderr to be unbuffered as stated by the doc:

  -u Force the binary I/O layers of stdout and stderr to be unbuffered. stdin is always buffered. The text I/O layer will still be line-buffered. 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM