Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

To further expand on the two (perfectly correct and valid) comments by almut and moritz above, here is a script that does exactly what you asked for:

#!/usr/bin/perl use warnings; use strict; use Parallel::ForkManager; use Sys::CPU; my $number_of_cpus = Sys::CPU::cpu_count(); my $pm = new Parallel::ForkManager($number_of_cpus); for (1..$number_of_cpus) { my $pid = $pm->start and next; my $string="abcdefg"; my $ln=length($string); print "length of a string = $ln \n"; $pm->finish(); }

This uses Sys::Cpu to find the number of CPUs the system has, then forks off that number of processes and executes the code in them. Note you'll get the output ("length of a string...") multiple times (depending on the number of processors you have in the system), because exactly the same code is run in each process. So this approach is rather nonsensical, it uses all processors (normally, depends on your OS) but simply multiplies the workload. Something more sensible would be to have each child measure a different string, e.g.:

#!/usr/bin/perl use warnings; use strict; use Parallel::ForkManager; use Sys::CPU; my $number_of_cpus = Sys::CPU::cpu_count(); my $pm = new Parallel::ForkManager($number_of_cpus); my @strings = ("abcde","abcdefghij","ab"); for (1..scalar @strings) { my $pid = $pm->start and next; my $string=$strings[$_-1]; my $ln=length($string); print "length of string \"$string\" = $ln \n"; $pm->finish(); }

This will hand each child a different string and thus "parallelize" the work done. This is slightly less senseless than the previous example, but only slightly, because the work done by your machine to fork off a separate process is much more than the simple act of measuring the length of a string. So, as the previous commenters have already pointed out, you need to do some thinking on exactly what you want to parallelize and how to do it in a worthwhile fashion. Anyway, hope this helps clear up some confusion.

All dogma is stupid.

In reply to Re: Parallel ForkManager by tirwhan
in thread Parallel ForkManager by aditya007

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others rifling through the Monastery: (6)
    As of 2018-06-20 15:59 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (116 votes). Check out past polls.