kwopublic has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to get my socket writes to be non-blocking. So far I have been unsuccessful. I am using an IO::Socket::UNIX type socket for interprocess communication. After opening it, I set the blocking flag to 0 but this does not seem to make my syswrite call non-blocking. Below are some snippets of my perl code and a print out from my logging. The logging clearly shows that the sywrite is blocking. Anyone have any idea how I can resolve this problem? Are UNIX sockets able to work in a non-blocking mode? Any help greatly appreciated, kwo
my $s = IO::Socket::UNIX->new( Type => SOCK_STREAM, Local => $sockname, Listen => 10, ReuseAddr => 1, ); if ( not $s ) { logerr("Can't create socket $sockname : $!"); exit $PACE::Constants::DAEMON_EXIT; } # Vantive ? : Set the Socket to non-blocking. $s->blocking(0); my $tmp_val = $s->blocking(); logdbg( 15, "Blocking flag = $tmp_val\n"); ... logdbg(14, "writeSocket : File # $fn has $size bytes to send.\n"); # if no data to write just return return if ($size == 0); $sent = syswrite $wh, $buf, $size; logdbg(14, "writeSocket : $size bytes attempted, $sent bytes writt +en.\n"); ... 06/01/31 15:49:56 Agent: writeSocket : File # 35 has 100098 bytes to s +end. 06/01/31 15:50:26 Agent: writeSocket : 100098 bytes attempted, 100098 +bytes written.

Replies are listed 'Best First'.
Re: How do I get my IO::Socket::UNIX sockets to be non-blocking?
by davido (Cardinal) on Feb 01, 2006 at 18:50 UTC

    Out of curiosity, what happens if you address blocking like this?

    $s->blocking(0) or die $!;

    I suppose it's possible that setting blocking off is failing, and this will let you know. It might not solve the issue, but at least you may discover where to dig.


    Dave

      The blocking interface returns the current value so the or die would not be appropriate.

        Are you sure? You may be right, but that's not what the documents say. The blocking() method is inherited from IO::Handle, from what I can tell, and the documentation for IO::Handle says the following:

        $io->blocking ( [ BOOL ] )
        If called with an argument blocking will turn on non-blocking IO if BOOL is false, and turn it off if BOOL is true.

        blocking will return the value of the previous setting, or the current setting if BOOL is not given.

        If an error occurs blocking will return undef and $! will be set.

        ...which should make it a good candidate for "or die $!;" syntax.


        Dave