Python Forum
wait for the first of these events - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: wait for the first of these events (/thread-36574.html)



wait for the first of these events - Skaperen - Mar-06-2022

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)?


RE: wait for the first of these events - ndc85430 - Mar-07-2022

(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).


RE: wait for the first of these events - Gribouillis - Mar-07-2022

Can you do a select() on the pipe with a timeout of 1 second?


RE: wait for the first of these events - Skaperen - Mar-07-2022

(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.


RE: wait for the first of these events - Gribouillis - Mar-07-2022

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 ?