set timeout module for Perl

A

Anio

Hi all, i need perl module for delay execution of code like setTimeout
(fn,ms) in Javascript does. Any suggestions?
 
J

Joost Diepenmaat

Anio said:
Hi all, i need perl module for delay execution of code like setTimeout
(fn,ms) in Javascript does. Any suggestions?

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.
 
J

Jürgen Exner

Anio said:
Hi all, i need perl module for delay execution of codes

See "perldoc -f sleep"
like setTimeout(fn,ms) in Javascript does.

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
 
A

Anio

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.

Thank you very much.
http://search.cpan.org/~mlehmann/EV-3.52/EV.pm is exactly what i need.
This prints the time in milliseconds for every loop pused in @timer
delayed by $interval/1_000 milliseconds:

my $interval = 50; #in milliseconds
my @timer;
for(my $i=0;$i < 100;$i++) {
push @timer, EV::timer $interval/1_000, 0, sub {
print EV::now,"\n";
};
}
EV::loop;
 
A

Anio

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

I dont want to run perl in browser. I want to execute some subs
delayed by time like in JS.
 
T

Ted Zlatanov

A> Hi all, i need perl module for delay execution of code like setTimeout
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
 
J

Jürgen Exner

Ted Zlatanov said:
Store the PID in a variable $pid.

No need for that, because ...
Fork. Now you have processes A (parent, PID=$pid) and B (child)).

.... 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.

jue
 
H

Hans Mulder

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.

Ted is storing the parent PID in a variable, so that the child knows
what process to send its signal to:

kill(USR1, $pid);

Of course he could also use the getppid function:

kill(USR1, getppid());

That might work better if his parent process has exited and the PID $pid
has been reassigned to a new process.

Hope this helps,

-- HansM
 
A

Anio

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

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?
 
J

Jürgen Exner

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);

Ok, I missed that. Thanks!

jue
 
J

Joost Diepenmaat

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?

You have to set $SIG{USR1} *before* fork()ing, since there is no
guarantee that 'kill "USR1",$parent;' is executed before the signal
handler is installed, otherwise.
 
T

Ted Zlatanov

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
 
A

Anio

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

Many thanks to all. Now all works as expected :)
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top