Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Threading Equivalent to this Parallel::ForkManager Example

by Anonymous Monk
on Apr 02, 2014 at 06:00 UTC ( [id://1080679]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Im looking for a threading (Threads) and/or (Thread:queue) Equivalent to my script that Basically this just runs through a list with different threads each thread doing a different variable from the array Example Below

use Parallel::ForkManager; my $max_processes = '10'; my $pm = Parallel::ForkManager->new($max_processes); my $array = 'list.txt'; open my $handle, '<', $array; chomp(my @array = <$handle>); close $handle; for my $printme (@array) { my $pid = $pm->start and next; print $printme; $pm->finish; } $pm->wait_all_children;

Replies are listed 'Best First'.
Re: Threading Equivalent to this Parallel::ForkManager Example
by BrowserUk (Patriarch) on Apr 02, 2014 at 07:27 UTC

    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.
Re: Threading Equivalent to this Parallel::ForkManager Example
by kcott (Archbishop) on Apr 02, 2014 at 07:49 UTC

    This would seem to be an equivalent:

    #!/usr/bin/env perl use strict; use warnings; use threads;
    while (<DATA>) { threads->create(sub { print 'Thread ', threads->tid, ': ', $_ })-> +join; }

    Update: Apparently not equivalent - see BrowserUk's comments below.

    __DATA__ list.txt line 1 list.txt line 2 list.txt line 3

    Output:

    Thread 1: list.txt line 1 Thread 2: list.txt line 2 Thread 3: list.txt line 3

    The "Perl Threads Tutorial" would probably be a good place to start if you want to learn more.

    -- Ken

      This would seem to be an equivalent:

      Not so. It is fatally flawed.

      Your code is directly equivalent to:

      #!/usr/bin/env perl use strict; use warnings; use threads; my $n = 0; while (<DATA>) { print 'Thread ', ++$n, ': ', $_; }

      Ie. It exhibits no parallelism whatsoever.


      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.

        Thanks for picking that up. I've updated my post.

        -- Ken

Re: Threading Equivalent to this Parallel::ForkManager Example
by zentara (Archbishop) on Apr 02, 2014 at 16:20 UTC

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2024-04-24 06:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found