Python Forum
reading from two or more pipes
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
reading from two or more pipes
#1
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:

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)
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?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
How about something like
import subprocess

ips = [...]
processes = [subprocess.Popen(['ping', '-c', '8', ip], stdout=subprocess.PIPE) for ip in ips]
while True:
   for process in processes:
       print(process.stdout.readline().strip())

   if all(process.poll() is not None for process in processes):
       exit()
(I realize you could exit with unread stdout here, but I'm hoping this code suggests how to solve your problem.)
Reply
#3
oooh, i didn't know you could do that with all() without [].  "other people's code" (opc) can very often help the learning process.

what i ultimately want to do is show differences between hosts, so i need to update the right stuff after each readline and do the appropriate output, like updating a graph (for example scaling other hosts up when one host has a long ping delay).
one task will be comparing pings outside a tunnel (between the tunnel endpoints) to pings inside the tunnel (between the tunnel interfaces).

yeah, i could merge all the responses an read it as one stream and check the ip addresses in the responses, but i really don't want to risk the issues with that.


i may need to have the code wake up on time events such as to have metrics updated due to the lack of a response.  but that can be done (if all else fails, add a ping to localhost).

now why did an empty line get added to my post?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how to run linux command with multi pipes by python !! evilcode1 2 6,325 Jan-25-2021, 11:19 AM
Last Post: DeaD_EyE
  Duplex pipes GrahamL 0 1,752 Dec-16-2020, 09:44 AM
Last Post: GrahamL
  waiting for the first of many pipes to send data Skaperen 7 3,982 May-05-2019, 10:57 PM
Last Post: Skaperen
  reading from 2 pipes Skaperen 2 69,633 Oct-05-2018, 04:45 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020