recurring timer in a standalone script

60 views
Skip to first unread message

Bhaskar Karambelkar

unread,
Jun 17, 2013, 3:54:11 PM6/17/13
to mojol...@googlegroups.com
I've a script which does some long running computations, and updates certain counter variables from time to time.
Ideally I would like to display these values from time to time. So here's what I tried

use Data::Dumper;
use Mojo::IOLoop;

my $loop = Mojo::IOLoop->singleton;

my $counterRef = {COUNTER1 => 0, COUNTER2 =>0,...} ;

$loop->recurring(1, sub { Dumper($counterRef) });

#$loop->start;

# Begin long running computation

$loop->end;

The problem is $loop->start is a blocking call. So I'm stuck, how do I start the loop w/o blocking ?

sri

unread,
Jun 17, 2013, 4:15:53 PM6/17/13
to Mojolicious
> The problem is $loop->start is a blocking call. So I'm stuck, how do I
> start the loop w/o blocking ?

You can't, that's not how event loops work.

--
sebastian

Bhaskar Karambelkar

unread,
Jun 17, 2013, 4:30:25 PM6/17/13
to mojol...@googlegroups.com, kra...@googlemail.com
Yep I realized that. Any tips on how I might go about in achieving what I need to do ?

anton.s...@yandex.ru

unread,
Jun 18, 2013, 2:19:39 AM6/18/13
to mojol...@googlegroups.com, kra...@googlemail.com
You can put long running task into $loop->timer( 0 => sub { <here> } )

вторник, 18 июня 2013 г., 0:30:25 UTC+4 пользователь Bhaskar Karambelkar написал:

Sebastian Riedel

unread,
Jun 18, 2013, 2:22:11 AM6/18/13
to mojol...@googlegroups.com
> You can put long running task into $loop->timer( 0 => sub { <here> } )

I think you meant to say "You can split your long running task into many smaller ones and put them <here>, one after another.".

--
Sebastian Riedel
http://twitter.com/kraih
http://mojolicio.us

Bhaskar Karambelkar

unread,
Jun 18, 2013, 10:49:12 AM6/18/13
to mojol...@googlegroups.com, kra...@googlemail.com
Yes I think you are right.
Coz if I try

my $loop = Mojo::IOLoop->singleton;

my $recID = $loop->recurring(1,sub { displayCounters; });

my $tID = $loop->timer(0,sub { <The whole of long running task>; $loop->stop });

$loop->start

It doesn't work. The timer task runs blocking, and the recurring task is never invoked.
I thought both the timer and the recurring tasks would run asynchronously in parallel.

thanks
Bhaskar
Reply all
Reply to author
Forward
0 new messages