Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

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

Hi again,

I made an improved version including worker sending the error message. Replace system with syscmd in your script. That sends the command along with any arguments to the background worker. The worker receives the command to run and any arguments. The status and error message are sent to the main process after running the command.

syscmd:

use strict; use warnings; use MCE::Child; use MCE::Channel; my $chnl = MCE::Channel->new( impl => 'Simple' ); # spin up worker early before creating big hash mce_child { local $SIG{__WARN__} = sub {}; while ( my ($cmd, @args) = $chnl->recv ) { local ($?, $!); system($cmd, @args); $chnl->send2($?, $!); } }; sub syscmd { my $cmd = shift; return unless $cmd; $chnl->send($cmd, @_); my ($status, $errmsg) = $chnl->recv2; if ($status == -1) { print "SYSTEM: failed to execute ($cmd): $errmsg\n"; } elsif ($status & 127) { printf "SYSTEM: $cmd died with signal %s, %s coredump\n", ($status & 127), ($status & 128) ? 'with' : 'without'; } else { printf "SYSTEM: $cmd exited with status %d\n", $status >> 8; } } # create big hash my %big_hash; # pass command and optionally args syscmd('ls'); # attempt to run a command not found syscmd('something'); # sleep for 2 seconds syscmd('sleep', '2'); # notify no more work, then reap worker $chnl->end; MCE::Child->waitall;

output:

ls output from syscmd SYSTEM: ls exited with status 0 SYSTEM: failed to execute (something): No such file or directory SYSTEM: sleep exited with status 0

Regards, Mario


In reply to Re^2: System call doesn't work when there is a large amount of data in a hash by marioroy
in thread System call doesn't work when there is a large amount of data in a hash by Nicolasd

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 wandering the Monastery: (5)
As of 2024-03-29 10:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found