Execute Jobs parallel using JobQueue

[email protected] (Ryan Chan)
Newsgroups perl.poe
Message-ID <[email protected]>
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.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.