Hello POE team,

I am trying to understand if the POE kernel dispatching algorithm has any
precedence when it comes to events from yield Vs events from delay.  Please
read through the email to see if you can help me figure out what is the
mistake i am doing here.

Following is the code snipett that demonstrates my issue.

<code>
#!/usr/bin/perl
use strict;
use warnings;
use Algorithm::TokenBucket;
use Time::HiRes;
use POE;
use POE::Component::Schedule ;

my $so_many_task = 10;
my $so_many_seconds = 1;
my $burst_size = 1;
my $stop = 0;

my $bucket = new Algorithm::TokenBucket $so_many_task / $so_many_seconds,
  $burst_size;

my $time           = Time::HiRes::time;
my $master_session = POE::Session->create(
    inline_states => {
        _start           => \&master_start,
        put_one_on_queue => \&put_one_on_queue,
        generate_tick    => \&generate_tick,
        next_task        => \&next_task,
        stop_scheduling  => \&stop_scheduling,
        _stop            => sub { },
        dummy            => sub { },
    }
);

POE::Kernel->run();
exit(0);

sub master_start {
    my $kernel = $_[KERNEL];
    $_[KERNEL]->alias_set("Enqueuer");
    $_[KERNEL]->yield("generate_tick");
    $_[KERNEL]->delay( "stop_scheduling" => 1 );
}


sub generate_tick {
    my $kernel = $_[KERNEL];
    if ( $stop == 0 ) {
        my $after_sec = $bucket->until($burst_size);
        print "Next one after $after_sec\n";
        $_[KERNEL]->delay_add( "put_one_on_queue" => $after_sec );
    }
}

sub stop_scheduling {
    print "Stopped \n";
}

sub put_one_on_queue {
    my $kernel = $_[KERNEL];
    if ( $bucket->conform($burst_size) ) {
        print Time::HiRes::time -$time, " - Processed $burst_size more \n";
        $bucket->count($burst_size);
        $_[KERNEL]->yield("generate_tick");
        $_[KERNEL]->yield("next_task");
    }
}

sub next_task {

    #--- some operation that is taking about 0.2 sec
    #--- using select to show it here.

    #print "Started next task\n";
    select( undef, undef, undef, 0.25 );
    #print "Done with next task\n";
}

</code>

<p>
With the above code and the select statement, which i amusing to simulate
some operation taking about .25 seconds, the output is as follows

[lss@ctsauto1 misc]$ ./atbkt_2.pl
Next one after 0.0995830059051514
0.100232124328613 - Processed 1 more
Next one after 0.0998860359191895
0.350847959518433 - Processed 1 more
Next one after 0.0998910427093506
0.60145115852356 - Processed 1 more
Next one after 0.0998910427093506
0.852055072784424 - Processed 1 more
Next one after 0.0998538494110107
1.10269498825073 - Processed 1 more
Stopped

*Well i was expecting 10 items to be processed in 1 second not just 5.*


Now if i comment the select statement in the next_task routine then I get
following output.

Next one after 0.0997329711914063
0.100254058837891 - Processed 1 more
Next one after 0.0999299049377441
0.200562953948975 - Processed 1 more
Next one after 0.0999120235443115
0.300816059112549 - Processed 1 more
Next one after 0.0999170303344727
0.401067018508911 - Processed 1 more
Next one after 0.0999160766601563
0.501316070556641 - Processed 1 more
Next one after 0.0999160766601563
0.601574897766113 - Processed 1 more
Next one after 0.0999120235443115
0.701813936233521 - Processed 1 more
Next one after 0.0999158382415772
0.802061080932617 - Processed 1 more
Next one after 0.0999151229858398
0.902349948883057 - Processed 1 more
Next one after 0.0999170303344727
Stopped
1.0025269985199 - Processed 1 more

*10 items in 1 second as expected.*


Any help will be greatly appreciated.

Thanks.
-- DP

Reply via email to