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

comment on

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

Hi Monks.

I have some perl reading messages from a socket and then taking action on them. The server end of the socket expects to get a "heartbeat" msg every so often. The actions sometimes take too long and the heartbeat is not sent so the code was converted to fork a child something like this:

pipe($child_reader, $child_writer); binmode($child_reader, ":encoding(UTF-8)"); binmode($child_writer, ":encoding(UTF-8)"); $child_reader->autoflush(1); $child_writer->autoflush(1); # fork child my $pid = fork; if (!defined($pid)) { warn "Failed to fork child process - turning off --fork_wr +iter"; return; } if ($pid) { # parent close $child_reader or warn "Parent: failed to close pipe +reader - $!"; return; } else { # child close $child_writer or warn "Child: failed to close pipe w +riter - $!"; child_writer(); exit; }

and all the child_writer() does is:

my $select = IO::Select->new(); $select->add(fileno $child_reader); $last_heartbeat_sent = gmtime(0); my $buf = ''; PROCESS: while (1) { my @ready = $select->can_read(5); if (scalar(@ready)) { my $read = sysread($child_reader, $buf, 64*1024, length($b +uf)); if (!defined($read)) { warn "Failed read on pipe to parent - $!"; last PROCESS; } elsif ($read == 0) { # EOF warn "EOF on pipe to parent"; last PROCESS; } else { while ($buf =~ s/^(.*)\r\n//) { send_message($1); # send msg down socket to server } } } else { my $now = gmtime; if (($now - $last_heartbeat_sent) > 25) { send_message("<Heartbeat/>\r\n"); $last_heartbeat_sent = $now; } } }

The problem is that although the above fixed the heartbeat issue the parent was still trying to do too much so now the parent forks other processes to handle different events received and they are all effectively writing something down the same pipe to the child above.. Occasionally what happens is I get an error writing to the pipe from code like this:

# $msg is the message to send, it is guaranteed < PIPE_BUF and + ends in \r\n my $sigpipe = 0; local $SIG{PIPE} = sub { $sigpipe = 1; }; my $sent = print $child_writer $msg; if (!$sent && ($!{EPIPE} || $sigpipe)) { warn "Write to child_writer failed with EPIPE - child died + ($sigpipe)"); # restarts child writer } else { warn "Write to child_writer failed - $!"; }

and the the last warning sometimes outputs "Write to child_writer failed - Operation now in progress". I don't understand this since the write end of the pipe is blocking and so how can I get EINPROGRESS errror? Any ideas?


In reply to "Operation now in progress" problem with a pipe by mje

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? | Other CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2022-12-03 12:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?