i got some example code online that showed me how to read from a pipe without using .communicate() which waits for the child process to exit, which may take a very long time for some things. i condensed the example and change some parts as shown:
i did not put in that blank line (line 10). my own file i ran to test the code does not have it and it works fine without it in both python 2 and python 3. it gets put in there every time i do Preview Post. what's up with that?
now what i want to do is run two or more processes (such as multiple pings) and read both from separate pipes so i know which process's output i am getting. i've done this a lot in C which involved the poll() or select() system calls to know which pipe had data ready to read. do i need to do the same in python or is there a library that provides this (prefer one that comes in python so others don't have to do some other install to use my program). any ideas, suggestions, or googling terms to learn what python can do along these lines?
1 2 3 4 5 6 7 8 9 10 11 12 |
from __future__ import print_function from sys import argv, stderr, stdout, version import subprocess def main(args): process = subprocess.Popen([ 'ping' , '-c' , '8' , '8.8.4.4' ], stdout = subprocess.PIPE) while True : output = process.stdout.readline() if len (output)< 1 and process.poll() ! = None : return print (output.strip()) if __name__ = = '__main__' : main(argv) |
now what i want to do is run two or more processes (such as multiple pings) and read both from separate pipes so i know which process's output i am getting. i've done this a lot in C which involved the poll() or select() system calls to know which pipe had data ready to read. do i need to do the same in python or is there a library that provides this (prefer one that comes in python so others don't have to do some other install to use my program). any ideas, suggestions, or googling terms to learn what python can do along these lines?
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.