A
Anio
Hi all, i need perl module for delay execution of code like setTimeout
(fn,ms) in Javascript does. Any suggestions?
(fn,ms) in Javascript does. Any suggestions?
Anio said:Hi all, i need perl module for delay execution of code like setTimeout
(fn,ms) in Javascript does. Any suggestions?
Anio said:Hi all, i need perl module for delay execution of codes
like setTimeout(fn,ms) in Javascript does.
Javascript in the browser uses events to trigger all actions, so if you
want to have it exactly like that, you'll need some event loop. See
EV, POE, Event, your GUI toolkit's documentation if you're using one and
whatever else turns up at
http://search.cpan.org/search?query=event&mode=all
Or you may want to use alarm() see perldoc -f alarm.
See "perldoc -f sleep"
You are aware that there are significant differences between Javascript
and Perl? Like Perl doesn't run in a web browser to begin with? You
could use Perlscript instead of Perl, but it is not widely used.
Aside of that if you are really looking for a timeout then please check
"perldoc -q timeout".
jue
Ted Zlatanov said:Store the PID in a variable $pid.
Fork. Now you have processes A (parent, PID=$pid) and B (child)).
Jürgen Exner said:No need for that, because ...
... the standard way to distinguish between parent and child is to check
the return value of fork(): childPID to the parent process and 0 to the
child process.
A> Hi all, i need perl module for delay execution of code likesetTimeout
A> (fn,ms) in Javascript does. Any suggestions?
I saw the other followups, but here's a simple way:
Store the PID in a variable $pid.
Fork. šNow you have processes A (parent, PID=$pid) and B (child)).
Do your action on the USR1 signal in A (usually this is done with
setting a variable that you check elsewhere).
In B, sleep for X milliseconds and then send the USR1 signal to the PID
in $pid you remembered from before.
Maybe this will help you if you want to avoid frameworks.
Ted
Hans Mulder said:Ted is storing the parent PID in a variable, so that the child knows
what process to send its signal to:
kill(USR1, $pid);
Anio said:Thanks for that. I tested it:
#########
my @lines = qw/foo bar baz/;
my @times = qw/0.2 0.25 0.34/;
for (my $i=0; $i<scalar @times; $i++) {
my $parent = $$;
unless (my $pid = fork()) {
select(undef, undef, undef, "$times[$i]");
kill "USR1",$parent;
exit();
}
}
$SIG{USR1} = sub {
print shift @lines, "\n"
};
<>; # to keep parent alive
###########
It work very good! But sometimes it exits with "User defined signal 1"
immediately and i cant understand why. Any ideas?
JD> You have to set $SIG{USR1} *before* fork()ing, since there is no
JD> guarantee that 'kill "USR1",$parent;' is executed before the signal
JD> handler is installed, otherwise.
I did not order things correctly, sorry Anio--and thanks, Joost, for
explaining.
HM> Ted is storing the parent PID in a variable, so that the child knows
HM> what process to send its signal to:
HM> š š kill(USR1, $pid);
HM> Of course he could also use the getppid function:
HM> š š kill(USR1, getppid());
HM> That might work better if his parent process has exited and the PID $pid
HM> has been reassigned to a new process.
Yes, that's better. šYou might want to avoid sending a USR1 signal to a
process that is not expecting it, so I'd save the parent PID anyway and
check that it's equal to getppid() before sending the signal.
Ted
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.