How Does One Implement a Timer in Perl?

K

kvnsmnsn

I need to write a Perl script for one server that sends off messages
on a socket and then waits for a response. If a response comes in
five seconds, it uses that response for its output. If a response
_doesn't_ come in five seconds, it uses another algorithm to generate
its output.

Is there a way in Perl to do this, to suspend execution until five
seconds have past or until a response has come, whichever happens
first? Any feedback on this would be greatly appreciated.

---Kevin Simonson

"You'll never get to heaven, or even to LA,
if you don't believe there's a way."
from _Why Not_
 
X

xhoster

I need to write a Perl script for one server that sends off messages
on a socket and then waits for a response. If a response comes in
five seconds, it uses that response for its output. If a response
_doesn't_ come in five seconds, it uses another algorithm to generate
its output.

Is there a way in Perl to do this, to suspend execution until five
seconds have past or until a response has come, whichever happens
first? Any feedback on this would be greatly appreciated.

IO::Select

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
 
J

Jürgen Exner

I need to write a Perl script for one server that sends off messages
on a socket and then waits for a response. If a response comes in
five seconds, it uses that response for its output. If a response
_doesn't_ come in five seconds, it uses another algorithm to generate
its output.

Your Question is Asked Frequently. See

perldoc -q timeout

"How do I timeout a slow event?"

jue
 
M

Martijn Lievaart

Using alarm introduces a potential race condition: 1. you set up an
alarm to fire in 5s
2. you request the response
3. the reponse arrives within 1s
4. dur to high load, further execution is delayed by 4.5s 5. the alarm
fires, you throw away a perfectly valid reply.

Just test for success first, for timeout later.

M4
 
P

Peter J. Holzer

["Followup-To:" header set to comp.lang.perl.misc.]
fishfry said:
Is there a way in Perl to [...] to suspend execution until five
seconds have past or until a response has come, whichever happens
first?

Your Question is Asked Frequently: perldoc -q timeout

However, that entry doesn't mention select, which seems to be more
appropriate to what the OP has in mind.

hp
 
J

Jürgen Exner

Peter J. Holzer said:
["Followup-To:" header set to comp.lang.perl.misc.]
fishfry said:
Is there a way in Perl to [...] to suspend execution until five
seconds have past or until a response has come, whichever happens
first?

Your Question is Asked Frequently: perldoc -q timeout

However, that entry doesn't mention select, which seems to be more
appropriate to what the OP has in mind.

???
You lost me. How does select help you to suspend execution for a
specific amount of time?

jue
 
B

Ben Morrow

Quoth Jürgen Exner said:
???
You lost me. How does select help you to suspend execution for a
specific amount of time?

select(undef, undef, undef, $timeout) is a standard way of doing
sub-second sleeps.

Ben
 
J

Jürgen Exner

Ben Morrow said:
select(undef, undef, undef, $timeout) is a standard way of doing
sub-second sleeps.

Thank you.

Maybe it's worth adding this tidbit to the FAQ entry for timeout?

jue
 
P

Peter J. Holzer

select(undef, undef, undef, $timeout) is a standard way of doing
sub-second sleeps.

Yep. But the real use of select is to wait (with a timeout) until I/O is
possible. And I understand this is what the OP wanted to do. Something
like (using the ooish IO::Select instead of the builtin):

use IO::Select;
...
$select = IO::Select->new()
$select->add($sock);
...
print $sock "ping\n";
my @fh = $select->can_read(5.0);
if (@fh) {
# server has answered, read the response
my $line = $fh[0]->readline;
} else {
# server didn't answer in 5 seconds.
}

This can easily be expanded to multiple I/O channels.

There are some caveats, though. Most importantly, select doesn't know
about buffers, so you may need to use sysread in some cases.

hp
 
B

brian d foy

Jürgen Exner said:
Maybe it's worth adding this tidbit to the FAQ entry for timeout?

Are you talking about "How do I timeout a slow event?" in perlfaq8?

How would select() work like that? You can pause, but I don't see how
you could let something else execute while you are doing it. What am I
missing? :)
 
P

Peter J. Holzer

Are you talking about "How do I timeout a slow event?" in perlfaq8?

How would select() work like that? You can pause, but I don't see how
you could let something else execute while you are doing it. What am I
missing? :)

Quite often you don't have something else to execute while waiting for
I/O. In this case using select saves the hassles associated with signals
(but you get the hassles assoiciated with unbuffered (and possibly
non-blocking) I/O instead).

hp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top