Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

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

Hi karlgoethebier,

The mce_open call behaves similarly to the native open in Perl. It creates a shared handle with exclusive locking handled transparently.

Example:

use strict; use warnings; use feature 'say'; use MCE::Hobo; use MCE::Shared; # Passing a file ref without IO::FDPass will work for \*STDIN, # \*STDOUT, \*STDERR, and \*main::DATA only. Otherwise, providing # the actual path is preferred (i.e. "/path/to/file"). mce_open my $out, '>>', \*STDOUT or die 'open failed: $!'; mce_open my $err, '>>', \*STDERR or die 'open failed: $!'; printf $out "shared fileno(\$out) is %d\n", fileno($out); printf $out "shared fileno(\$err) is %d\n", fileno($err); if ($^O ne 'MSWin32') { mce_open my $log, '>>', '/dev/null' or die 'open failed: $!'; say $log "Hello, there!"; # sent to null } # The shared-handles work with threads, MCE::Hobo, MCE::Child, # Parallel::ForkManager, and other parallel modules on CPAN. # Note: There is no reason to choose MCE::Child over MCE::Hobo # if already involving the shared-manager. sub foo { my ($id) = @_; say $out "Hello, from pid $$"; } MCE::Hobo->create('foo', $_) for 1..4; MCE::Hobo->wait_all;

Output:

shared fileno($out) is 1 shared fileno($err) is 2 Hello, from pid 4651 Hello, from pid 4652 Hello, from pid 4653 Hello, from pid 4654

See also:

https://metacpan.org/pod/MCE::Shared#DBM-SHARING
https://metacpan.org/pod/MCE::Shared#LOGGER-DEMONSTRATION

Regards, Mario


In reply to Re^3: "open" Best Practices by marioroy
in thread "open" Best Practices by haukex

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 cooling their heels in the Monastery: (3)
As of 2024-03-29 04:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found