How can I tell Perl to run some code every 20 seconds?

后端 未结 8 826
余生分开走
余生分开走 2021-02-13 15:45

How can I tell Perl to run some code every 20 seconds?

8条回答
  •  醉酒成梦
    2021-02-13 16:34

    It is better to use some event library. There are a couple of choices:

    IO::Async::Timer::Periodic

    use IO::Async::Timer::Periodic;
    
    use IO::Async::Loop;
    my $loop = IO::Async::Loop->new;
    
    my $timer = IO::Async::Timer::Periodic->new(
       interval => 60,
    
       on_tick => sub {
          print "You've had a minute\n";
       },
    );
    
    $timer->start;
    
    $loop->add( $timer );
    
    $loop->run;
    

    AnyEvent::DateTime::Cron

    AnyEvent::DateTime::Cron->new()
        ->add(
            '* * * * *'   => sub { warn "Every minute"},
            '*/2 * * * *' => sub { warn "Every second minute"},
          )
        ->start
        ->recv
    

    EV::timer

    etc.

提交回复
热议问题