Discussion:
How Does One Implement a Timer in Perl?
(too old to reply)
k***@hotmail.com
2007-11-06 17:51:05 UTC
Permalink
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***@gmail.com
2007-11-06 17:56:56 UTC
Permalink
Post by k***@hotmail.com
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ürgen Exner
2007-11-06 18:24:43 UTC
Permalink
Post by k***@hotmail.com
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
jordilin
2007-11-07 08:10:08 UTC
Permalink
Post by Jürgen Exner
Post by k***@hotmail.com
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
Take a look at
http://www.perl.com/doc/manual/html/pod/perlfunc/alarm.html
hope that helps,
jordi
Josef Moellers
2007-11-07 08:59:42 UTC
Permalink
Post by jordilin
Post by Jürgen Exner
Post by k***@hotmail.com
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
Take a look at
http://www.perl.com/doc/manual/html/pod/perlfunc/alarm.html
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.
--
These are my personal views and not those of Fujitsu Siemens Computers!
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize (T. Pratchett)
Company Details: http://www.fujitsu-siemens.com/imprint.html
Martijn Lievaart
2007-11-07 22:32:07 UTC
Permalink
Post by jordilin
Take a look at
http://www.perl.com/doc/manual/html/pod/perlfunc/alarm.html
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
fishfry
2008-06-13 05:37:14 UTC
Permalink
Post by k***@hotmail.com
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_
If you're on a *nix-like system (linux, MacOS, Solaris, etc.) you can
use signals. I don't know if those work on other platforms. The idea is
that you write a function called a signal handler. Then you set a timer.
When the timer pops, control transfers to your signal handler.
Jürgen Exner
2008-06-13 11:11:12 UTC
Permalink
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

jue
Peter J. Holzer
2008-06-14 08:38:50 UTC
Permalink
["Followup-To:" header set to comp.lang.perl.misc.]
Post by Jürgen Exner
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

Loading...