Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Threading Equivalent to this Parallel::ForkManager Example

by BrowserUk (Pope)
on Apr 02, 2014 at 07:27 UTC ( #1080690=note: print w/ replies, xml ) Need Help??


in reply to Threading Equivalent to this Parallel::ForkManager Example

Starting a whole thread just print a value is silly, but no more so than forking a process I suppose.

Notionally a direct equivalent:

#! perl -slw use strict; use threads; use threads::shared; my $sem :shared; our $MAX //= 10; my $array = 'list.txt'; open my $handle, '<', $array; chomp(my @array = <$handle>); close $handle; my @threads; for my $printme ( @array ) { push @threads, async{ lock $sem; print $printme; }; sleep 1 while threads->list( threads::running ) > $MAX; } $_->join for @threads;

A simplistic thread pool solution:

#! perl -slw use strict; use threads; use threads::shared; use Thread::Queue; our $MAX //= 10; my $sem: shared; my $Q = new Thread::Queue; sub thread { while( my $printme = $Q->dequeue ) { lock $sem; print $printme; } } my @threads = map async( \&thread ), 1 .. $MAX; my $array = 'list.txt'; open my $handle, '<', $array; chomp, $Q->enqueue( $_ ) while defined( $_ = <$handle> ); close $handle; $Q->enqueue( (undef) x $MAX ); $_->join for @threads;

(Neither tested).


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.


Comment on Re: Threading Equivalent to this Parallel::ForkManager Example
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (12)
As of 2014-09-16 17:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (40 votes), past polls