The other general advice is to use fork(), with or without POE, when you need true parallelism.

--
Rocco Caputo - rcap...@pobox.com


On Dec 1, 2009, at 08:50, Mark Morgan wrote:

Good day, Ryan,

The problem with using 'sleep' in POE code is that POE isn't true
pre-emptive multi-tasking, but rather cooperative multitasking at the
process level.  Different sessions are still running in the same
process/thread, so any actions that will monopolize CPU time for a
stretch will block all POE tasks.  The general advice for POE tasks is
that they should do no blocking calls in any individual event
handling, in order to allow other sessions to run when the current one
is idle.

The best way to have a session delay itself for a time is one of the
methods described in
http://search.cpan.org/~rcaputo/POE-1.280/lib/POE/Kernel.pm#Timer_Events_%28Delayed_Messages%29
.

Take care,
Mark.

On Tue, Dec 1, 2009 at 4:38 PM, Ryan Chan <ryanchan...@gmail.com> wrote:
Hello,

Consider my code below would like to execute the sleep() function in
parallel, using POE JobQueue component:


#=============
use strict;
use POE qw(Component::JobQueue);

# Passive queue waits for enqueue events.
POE::Component::JobQueue->spawn(
       Alias       => 'passive',
       WorkerLimit => 16,
       Worker      => \&spawn_a_worker,
       Passive => {
               Prioritizer => sub { 1 }
       }
);

sub spawn_a_worker {
       my ( $postback, @job_params ) = @_;
       POE::Session->create(
               inline_states => {
                       _start => \&start,
                       sleep  => \&sleep
               },
               args => [
                       $postback,
                       @job_params,
               ],
       );
}

POE::Session->create(
       inline_states => {
               _start => \&init,
               _stop  => sub { print "END" }
       }
);
POE::Kernel->run();
exit(0);

sub init {
       my $kernel = $_[KERNEL];

       foreach ( 1 .. 10 ) {
               $kernel->post( passive => enqueue => response => $_ );
       }
}

sub start {
       my $kernel = $_[ KERNEL ];
       print "Starting\n";
       $kernel->yield("sleep");
}

sub sleep {
       print "Sleeping for 5 seconds\n";
       sleep 5;
}

#==========

It seems that tasks still block each other? If not, I would have all
message executed at the end of 5 seconds.


Reply via email to