Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^3: Perl threads loss of performance with system call

by Anonymous Monk
on Jul 14, 2021 at 06:09 UTC ( [id://11134987]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Perl threads loss of performance with system call
in thread Perl threads loss of performance with system call

The channel object is bidirectional. Therefore, the worker can send its PID value to the producer. This has the effect of max one item in the channel. The producer writes to STDOUT like the demonstration by karlgoethebier.

#!/usr/bin/env perl use strict; use warnings; use MCE::Hobo; use MCE::Channel; use feature qw(say); use constant AMOUNT => 0.001; my $cores = MCE::Util::get_ncpu(); my $queue = MCE::Channel->new(); MCE::Hobo->init( void_context => 1, posix_exit => 1, ); # Consumers sub consumer_task { while ( my $input = $queue->recv() ) { my $id = $input->{id}; $queue->send2( MCE::Hobo->pid() ); qx( sleep @{[ AMOUNT ]};) } } MCE::Hobo->create( \&consumer_task ) for 1..$cores; # Producer for my $id ( 1..800 ) { $queue->send({ id => $id }); my $hobo_pid = $queue->recv2(); say qq($id ) . $hobo_pid; } $queue->end(); MCE::Hobo->wait_all();

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11134987]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2024-04-26 08:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found