How Does One Implement a Timer in Perl?

How Does One Implement a Timer in Perl?

am 06.11.2007 18:51:05 von 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_

Re: How Does One Implement a Timer in Perl?

am 06.11.2007 18:56:56 von xhoster

kvnsmnsn@hotmail.com wrote:
> 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.

Re: How Does One Implement a Timer in Perl?

am 06.11.2007 18:56:56 von xhoster

kvnsmnsn@hotmail.com wrote:
> 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.

Re: How Does One Implement a Timer in Perl?

am 06.11.2007 19:24:43 von jurgenex

kvnsmnsn@hotmail.com wrote:
> 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

Re: How Does One Implement a Timer in Perl?

am 06.11.2007 19:24:43 von jurgenex

kvnsmnsn@hotmail.com wrote:
> 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

Re: How Does One Implement a Timer in Perl?

am 07.11.2007 09:10:08 von jordilin

On Nov 6, 6:24 pm, "Jürgen Exner" wrote:
> kvnsm...@hotmail.com wrote:
> > 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

Re: How Does One Implement a Timer in Perl?

am 07.11.2007 09:10:08 von jordilin

On Nov 6, 6:24 pm, "Jürgen Exner" wrote:
> kvnsm...@hotmail.com wrote:
> > 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

Re: How Does One Implement a Timer in Perl?

am 07.11.2007 09:59:42 von Josef Moellers

jordilin wrote:
> On Nov 6, 6:24 pm, "Jürgen Exner" wrote:
>=20
>>kvnsm...@hotmail.com wrote:
>>
>>>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
>=20
>=20
> 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.
--=20
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

Re: How Does One Implement a Timer in Perl?

am 07.11.2007 09:59:42 von Josef Moellers

jordilin wrote:
> On Nov 6, 6:24 pm, "Jürgen Exner" wrote:
>=20
>>kvnsm...@hotmail.com wrote:
>>
>>>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
>=20
>=20
> 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.
--=20
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

Re: How Does One Implement a Timer in Perl?

am 07.11.2007 23:32:07 von Martijn Lievaart

On Wed, 07 Nov 2007 09:59:42 +0100, Josef Moellers wrote:

> jordilin wrote:
>> 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

Re: How Does One Implement a Timer in Perl?

am 07.11.2007 23:32:07 von Martijn Lievaart

On Wed, 07 Nov 2007 09:59:42 +0100, Josef Moellers wrote:

> jordilin wrote:
>> 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