Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^5: Timed event within a script

by Marshall (Canon)
on Mar 10, 2017 at 21:29 UTC ( [id://1184227]=note: print w/replies, xml ) Need Help??


in reply to Re^4: Timed event within a script
in thread Timed event within a script

Geez. One can only hope that this "emergency message" doesn't mean something like "reactor coolant low"!

To make this into a single process, you will need to determine whether or not the socket for the device has a line for you to process in it or not. I think that you will need IO::Socket. This sort of thing would be done in what is called a "select server". Don't read from the socket unless you know that it has something to say.

I think this psuedo code would be fine in your application.

# open file handles for reading and reporting # set $since_report_secs (seconds) = 0 while (sleep (1)) { my $emergency = check_for_emergency(); if ($emergency) { do something; } report_awake_status(); } sub check_for_emergency { while (device port ready to read) { read line and check it for "emergency" return 1 if emergency; } return 0; # "no error"; } sub report_awake_status { $last_report_time++; if (more than 30 minutes) { send awake message, set counter back to zero } }
The above code essentially sets up a "polling loop". This is not as efficient as it "could be". But the logic for a simple single process is simple and I think that you will find that code like this doesn't take much CPU time.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1184227]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2024-04-23 21:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found