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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.