in reply to Non-blocking Reads from Pipe Filehandle
I'm trying to do this as simply as possible, ... I'd like to avoid having to use ..., threads, ...
Shame, because that's by far the easiest way to do it. The structure of your main loop is almost exactly as you envisioned it and it just works:
#! perl -slw use strict; use threads; use Thread::Queue; sub pipeCommand { my $cmd = shift; my $Q = new Thread::Queue; async{ my $pid = open my $pipe, $cmd or die $!; $Q->enqueue( $_ ) while <$pipe>; $Q->enqueue( undef ); }->detach; return $Q; } my $pipe = pipeCommand( 'perl -le"$|++;print localtime().q[: some text] and sleep 1 for 1 +.. 10" |' ) or die; while( 1 ) { if( $pipe->pending ) { my $line = $pipe->dequeue or last; chomp( $line ); ## Do stuff with $line printf "Got: '%s'\n", $line; } else { ## Do something else print 'Tum te tum'; Win32::Sleep 500; } } __END__ C:\test>621058-t Tum te tum Got: 'Thu Jun 14 01:43:13 2007: some text' Tum te tum Got: 'Thu Jun 14 01:43:14 2007: some text' Tum te tum Tum te tum Got: 'Thu Jun 14 01:43:15 2007: some text' Tum te tum Tum te tum Got: 'Thu Jun 14 01:43:16 2007: some text' Tum te tum Tum te tum Got: 'Thu Jun 14 01:43:17 2007: some text' Tum te tum Tum te tum Got: 'Thu Jun 14 01:43:18 2007: some text' Tum te tum Tum te tum Tum te tum Got: 'Thu Jun 14 01:43:19 2007: some text' Tum te tum Got: 'Thu Jun 14 01:43:20 2007: some text' Tum te tum Tum te tum Got: 'Thu Jun 14 01:43:21 2007: some text' Tum te tum Tum te tum Got: 'Thu Jun 14 01:43:22 2007: some text' Tum te tum Tum te tum
To my knowledge, there is no way to put a win32 anonymous pipe into non-blocking mode. There is an obscure reference that's suggests it might be possible to set the read-end non-blocking using SetNamedPipeHandleState if it has been opened using ReadFile() (See the section on PIPE_NOWAIT and the reference to LAN Manager but I have never made this work, so WaitMultipleObjects() probably isn't going to help.
I did assist demerphq with reference to IO::Pipe a year or so ago, and succeeded in getting PeekNamedPipe to work with that, which allows you to poll the pipe. (See the bit that says the call will also work with anonymous pipes.)
I've tried that with a normal pipe handle, but can't make it work (yet!). The call to PeekNamedPipe() isn't giving any errors, but it's not telling me there is data available either. I seem to remember having this problem last time initially, but I can't remember how I fixed it. I no longer have the code, but I'll message demerphq and hope that he still has it.
But even if it can be made to work, it is hardly less complicated:
#! perl -slw use strict; use Win32API::File qw[ GetOsFHandle ]; use Win32::API::Prototype; ApiLink( 'Kernel32', q[ BOOL PeekNamedPipe( HANDLE hNamedPipe, LPVOID lpBuffer, DWORD nBufferSize, LPDWORD lpBytesRead, DWORD *lpTotalBytesAvail, LPDWORD lpBytesLeftThisMessage ) ] ) or die $^E; my $cmd = 'perl -le"$|++;print localtime().q[: some text] and sleep 1 +for 1..10" |'; my $pid = open my $pipe, $cmd or die $!; warn $pid; my $pHandle = GetOsFHandle( $pipe ); warn $pHandle; while( 1 ) { my $cAvail = 0; if( ( PeekNamedPipe( $pHandle, 0, 0, 0, $cAvail, 0 ) or warn $^E ) + and $cAvail ) { defined( my $line = <$pipe> ) or last; chomp( $line ); ## Do stuff with $line printf "Got: '%s'\n", $line; } else { ## Do something else print 'Tum te tum'; Win32::Sleep 500; } }
So, a couple of choices. One easy that works now. One less easy that only might work. Your choice.
Also, maybe the code I wrote for IO::Pipe made it into the 5.9.x releases. Maybe you could use bleed perl an that?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Non-blocking Reads from Pipe Filehandle
by ikegami (Patriarch) on Sep 30, 2008 at 23:23 UTC | |
by BrowserUk (Patriarch) on Oct 01, 2008 at 01:04 UTC | |
by ikegami (Patriarch) on Oct 01, 2008 at 02:07 UTC | |
by BrowserUk (Patriarch) on Oct 01, 2008 at 03:40 UTC | |
by ikegami (Patriarch) on Oct 01, 2008 at 04:00 UTC | |
| |
by ikegami (Patriarch) on Oct 01, 2008 at 06:25 UTC | |
| |
by BrowserUk (Patriarch) on Oct 01, 2008 at 02:31 UTC | |
|
Re^2: Non-blocking Reads from Pipe Filehandle
by cbudin (Initiate) on Jun 15, 2007 at 02:01 UTC | |
by BrowserUk (Patriarch) on Jun 15, 2007 at 10:19 UTC |