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
-
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.