Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Hi karlgoethebier,

Let's imagine for a minute, the following statement.

$mutex->enter( $shared->set( $_ => $fetch->($_) ) ); 1. the worker enters a mutex meaning one worker runs solo while inside the mutex 2. then does a fetch on given URL 3. then stores the result into a shared hash 4. finally, leaves the mutex

The statement above is causing MCE workers to run serially, not parallel. I've gone back to your earlier example here and that looks fine. However for this thread, maybe running solo is what karlgoethebier intended and respecting his decision to do so. Surely, he wanted the code to run parallel ;-).

mce_loop { MCE->yield; # run parallel my $url = $_; my $result = $fetch->($url); # run solo to store the result $mutex->enter( $shared->set( $url => $result ) ); # am back to running parallel # ... }

A mutex isn't needed when IPC involves a single trip, typical for the OO interface.

mce_loop { MCE->yield; # run parallel, without a mutex $shared->set( $_ => $fetch->($_) ); }

A mutex is often necessary for a shared hash when constructed via the TIE interface.

tie my %hash, 'MCE::Shared'; my $shared = MCE::Shared->hash(); my $mutex = MCE::Mutex->new(); $hash{number} = 0; # 1 trip, store $shared->set( number => 0 ); # 1 trip # 2 trips fetch and store, needs a mutex $mutex->enter( $hash{number} += 2 ); # 1 trip via the OO interface $shared->incrby( number => 2 );

Regards, Mario


In reply to Re: Yet another example to get URLs in parallel by marioroy
in thread Yet another example to get URLs in parallel by karlgoethebier

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (6)
As of 2024-04-19 10:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found