set timeout module for Perl

Discussion in 'Perl Misc' started by Anio, Jan 13, 2009.

  1. Anio

    Anio Guest

    Hi all, i need perl module for delay execution of code like setTimeout
    (fn,ms) in Javascript does. Any suggestions?
    Anio, Jan 13, 2009
    #1
    1. Advertising

  2. Anio <> writes:

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

    --
    Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
    Joost Diepenmaat, Jan 13, 2009
    #2
    1. Advertising

  3. Anio <> wrote:
    >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
    Jürgen Exner, Jan 13, 2009
    #3
  4. Anio

    Anio Guest

    On Jan 13, 4:35 pm, Joost Diepenmaat <> wrote:
    > Anio <> writes:
    > > 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.
    >
    > --
    > Joost Diepenmaat | blog:http://joost.zeekat.nl/| work:http://zeekat.nl/


    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;
    Anio, Jan 13, 2009
    #4
  5. Anio

    Anio Guest

    On Jan 13, 6:11 pm, Jürgen Exner <> wrote:
    > Anio <> wrote:
    > >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


    I dont want to run perl in browser. I want to execute some subs
    delayed by time like in JS.
    Anio, Jan 13, 2009
    #5
  6. Anio

    Ted Zlatanov Guest

    On Tue, 13 Jan 2009 06:00:51 -0800 (PST) Anio <> wrote:

    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
    Ted Zlatanov, Jan 14, 2009
    #6
  7. Ted Zlatanov <> wrote:
    >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
    Jürgen Exner, Jan 14, 2009
    #7
  8. Anio

    Hans Mulder Guest

    Jürgen Exner wrote:
    > Ted Zlatanov <> wrote:
    >> 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.


    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
    Hans Mulder, Jan 14, 2009
    #8
  9. Anio

    Anio Guest

    On 14 ñÎ, 22:09, Ted Zlatanov <> wrote:
    > On Tue, 13 Jan 2009 06:00:51 -0800 (PST) Anio <> wrote:
    >
    > 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?
    Anio, Jan 14, 2009
    #9
  10. Hans Mulder <> wrote:
    >Jürgen Exner wrote:
    >> Ted Zlatanov <> wrote:
    >>> 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.

    >
    >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ürgen Exner, Jan 15, 2009
    #10
  11. Anio <> writes:

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

    --
    Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
    Joost Diepenmaat, Jan 15, 2009
    #11
  12. Anio

    Ted Zlatanov Guest

    On Thu, 15 Jan 2009 12:47:32 +0100 Joost Diepenmaat <> wrote:

    JD> Anio <> writes:
    >> 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.

    On Thu, 15 Jan 2009 00:28:52 +0100 Hans Mulder <> wrote:

    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
    Ted Zlatanov, Jan 15, 2009
    #12
  13. Anio

    Anio Guest

    On 15 ñÎ, 19:47, Ted Zlatanov <> wrote:
    > On Thu, 15 Jan 2009 12:47:32 +0100 Joost Diepenmaat <> wrote:
    >
    > JD> Anio <> writes:
    > >> 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.
    >
    > On Thu, 15 Jan 2009 00:28:52 +0100 Hans Mulder <> wrote:
    >
    > 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 :)
    Anio, Jan 15, 2009
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bob Johnson
    Replies:
    0
    Views:
    3,738
    Bob Johnson
    Aug 7, 2003
  2. Do
    Replies:
    2
    Views:
    6,348
  3. Guoqi Zheng
    Replies:
    4
    Views:
    13,011
    Guoqi Zheng
    Jun 3, 2004
  4. Mark Probert

    Timeout::timeout and Socket timeout

    Mark Probert, Oct 6, 2004, in forum: Ruby
    Replies:
    1
    Views:
    1,272
    Brian Candler
    Oct 6, 2004
  5. roadrunner
    Replies:
    1
    Views:
    1,057
    Ben Morrow
    Oct 13, 2007
Loading...

Share This Page