That is, let the event loop process the events currently in queue. In VBA the call is named DoEvents
.
I want to do this in Perl. Preferably to do this in AnyEvent, because I have a script written with it. But I couldn't find any function like that in documentation.
I naively tried to implement it with a condvar, because documentation says that recv
calls event loop, but it failed, here is sample code:
use strict;
use warnings;
use AnyEvent;
use AnyEvent::Strict;
sub _long_task;
my $t1 = AE::timer 0, 3, sub{print"task 1, with interval 3 seconds\n";};
my $t2 = AE::timer 0, 7, sub{print"task 2, with interval 7 seconds\n";};
my $t3 = AE::timer 0, 11, sub{print"task 3, with interval 11 seconds\n";};
my $t_long = AE::timer 0, 0, \&_long_task;
sub DoEvents()
{
my $cv = AE::cv;
my $t = AE::timer 0, 0, sub { $cv->send; };
$cv->recv;
}
sub _long_task {
print"long task: ENTERING\n";
for(1..5) {
print"long task: doing some work for 2 seconds\n";
sleep 2;
print"long task: calling DoEvents\n";
DoEvents();
}
print"long task: EXITING, resheduling after 10 seconds\n";
$t_long = AE::timer 10, 0, \&_long_task;
}
AE::cv->recv;
The output is:
task 1, with interval 3 seconds
task 2, with interval 7 seconds
task 3, with interval 11 seconds
long task: ENTERING
long task: doing some work for 2 seconds
long task: calling DoEvents
AnyEvent::CondVar: recursive blocking wait attempted at C:\Users\andreyi\Desktop\try_event_loop.pl line 18.
UPDATE: There are lines in AnyEvent.pm:
$WAITING
and Carp::croak "AnyEvent::CondVar: recursive blocking wait attempted";
If you comment them, the DoEvents() works.
However, it will be better to have solution that does not involve mofidication of this CPAN module.