Hi all,
This is my virgin question to perlmonks!
I am aiming to write a funtion that impelements a timeout facility.
I have done all the code for the timeout with "eval" "$SIG{ALRM}" "alarm" and I have proven the actual timeout code.
The alarm gets set to a number of seconds, then the next bit of code is where I perform an operation that might hang the script.
What I would like to do is re-use the timeout code by putting it into a timeout funtion, where I pass in the timeout time, address of a funtion and any parameters that need to be passed to that funtion.
In the timeout function, I intend to do the "eval" "$SIG{ALRM}" "alarm" code and then call the funtion that the timeout funtion has been given the address of via the \&<function name> mechanism. This \&<function
name> would be a function that might hang.
E.G.
sub timeout_func{
# The first parameter is the timeout time
my $TIMEOUT=sprintf("%s",$_[0]);
# The second parameter is the address of the function we
# need to perform a timeout for
my $ADDRESS=sprintf("%s",$_[1]);
# The third parameter is the first parameter of the
# function we need to run
my $FIRST=sprintf("%s",$_[2]);
eval{
#set the callback for the alarm signal
local $SIG{ALRM} = sub { die ("ETIME\n") };
#specify the alarm timeout
alarm($TIMEOUT);
#call the funtion that the calling code has provided
#the address of via the \&<function name> mechanism
#if we came back with in the time limit, reset the alarm
alarm(0);
};
} # timeout_func
timeout_func(120,\&funtion_that_might_hang,<1st param to
funtion_that_might_hang>);
I realise that I stil need to do more work on this code, but the bit I
am hoping that someone can advise me on how to do the following:
#call the funtion that the calling code has provided the
#address of via the \&<function name> mechanism
Please could anybody let me know if they know how I should do this.
I know that the \&<function name> mechanism can be made to work becasue the threads and forks modules use this method.
If I can do this, then I can use this timeout funtion in various places in my scripts without duplicatind the same code.
Thanks in advance.
Richard Thomas
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.