Python Forum
wait for the first of these events
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
wait for the first of these events
#1
i want to wait for the first event to happen of these two events

1. the time one second in the future. just doing time.sleep() is not usable since it cannot wake up on other events while asleep.

2. data sent over a pipe written to the other end by a child process. i need to read this data as soon as it is written. this data is the output from the ping command.

the use case is to immediately detect when there is an N second gap between the ping command results.

the first idea i had was two processes doing the writing over the same pipe. that way my code can just do a read from the pipe and get whatever comes next. i just need to make the data distinguishable, which is trivial to do (make the time not look like ping output). the issue for me is that it seems rather wasteful to use a whole process to write the time every second.

the one thing i find so difficult in every programming model i have seen is the ability to wait for two different events to immediately ake up on which event happens next without knowing which it will be. can Python handle this with just one child process (ping)?
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
(Mar-06-2022, 11:12 PM)Skaperen Wrote: the one thing i find so difficult in every programming model i have seen is the ability to wait for two different events to immediately ake up on which event happens next without knowing which it will be.

While not Python related, Clojure's channels let you do that (here). It looks like you can do the same in Go with goroutines (here).
Reply
#3
Can you do a select() on the pipe with a timeout of 1 second?
Reply
#4
(Mar-07-2022, 07:51 AM)Gribouillis Wrote: Can you do a select() on the pipe with a timeout of 1 second?
i'm sure i can. i've done that a lot in C and a couple times in Python. but i am tying to avoid system specific code as much as i can.

at that point why not just send the ping request packet and wait for the ping reply? one fewer process. if i'm thinking in system terms enough to do os.select() then one fewer process has value.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#5
Skaperen Wrote:i am tying to avoid system specific code as much as i can.

I think the "pipe" terminology is confusing. If you start a child process with child = subprocess.Popen(...) and you wait for incoming data from child.stdout, this file object should work in the selectors module, so you could write OS agnostically (untested)
sel = selectors.DefaultSelector()
sel.register(child.stdout, selectors.EVENT_READ)
pairs = sel.select(timeout=1)
if pairs:
    data = child.stdout.readline()  # or perhaps read(1024) ?
    ...
else:
    # timed out
    # terminate child process ?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Wait til a date and time KatManDEW 2 1,390 Mar-11-2022, 08:05 PM
Last Post: KatManDEW
  how to do a two-way wait Skaperen 2 1,229 Mar-04-2022, 02:31 AM
Last Post: Skaperen
  Run an app in a standalone terminal and wait until it's closed glestwid 2 2,447 Aug-30-2020, 08:14 AM
Last Post: glestwid
  python os.popen is not working for wait method elenaflorence87 0 1,969 Jul-22-2020, 12:56 PM
Last Post: elenaflorence87
  Wait for command within a process bRitch022 1 3,183 Jul-15-2020, 07:03 PM
Last Post: bRitch022
  Simulating events using Hawkes akshit2291 1 2,115 Sep-25-2018, 04:17 AM
Last Post: Larz60+
  win32com Events not catching dageci 0 3,720 Aug-06-2018, 03:18 PM
Last Post: dageci
  Subprocess.send_signal, wait until completion plinio 5 8,913 Jun-29-2018, 12:07 PM
Last Post: plinio
  just a trial. Please wait 5 minutes before deleting this thread sylas 2 3,357 Jun-06-2017, 03:34 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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