Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Threading with a twist

by ikegami (Patriarch)
on Oct 03, 2017 at 07:46 UTC ( [id://1200580]=note: print w/replies, xml ) Need Help??


in reply to Threading with a twist

As previously mentioned, process STDIN and $rx_q in separate threads. But also, use dequeue instead of dequeue_nb.

use strict; use warnings qw( all ); use threads; use Thread::Queue qw( ); # 3.01+ use constant NUM_WORKERS => 10; sub worker { my ($job) = @_; return "[$job]"; } sub collector { my ($job) = @_; print "{$job}\n"; } { my $tx_q = Thread::Queue->new(); my $rx_q = Thread::Queue->new(); my @workers; for (1..NUM_WORKERS) { push @workers, async { while (defined( my $job = $tx_q->dequeue() )) { $rx_q->enqueue(worker($job)); } }; } my $collector_thread = async { while (defined( my $job = $rx_q->dequeue() )) { collector($job); } }; while (my $job = <>) { chomp($job); $tx_q->enqueue($job); } $tx_q->end(); $_->join() for @workers; $rx_q->end(); $collector_thread->join(); }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2024-04-26 06:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found