--Boundary_(ID_SN89RqUY92L6wO8WxD+/gg)
Content-type: text/plain
Content-transfer-encoding: 7BIT
I've been attempting to migrate some of my ptk tools from unix to win32 and
I've run into a bit of a problem. Several of my tools execute external
scripts via pipes. This all works great on unix using methods discussed some
time ago on this list. However, now that I'm migrating some of these to
win32 I'm running into an IO blocking problem. I've posted this to the
active state perl list with no successful resolution. So I'm now posting to
the ptk list. Has anybody been able to establish non-blocking IO in perl on
win32, and could you tell me how to implement it?
I'm including my current external command routine. It works great on unix,
but blocks until execution is complete on win32.
Thanks, Dave.
sub syscmd {
print "TRACE: syscmd\n" if ($trace == 1);
use FileHandle;
($cmd,$state) = @_;
$pipe = new FileHandle; $|=1;
$pipe->open("$cmd|");
# $pipe->autoflush(1); # this didn't work anybetter than the "$|=1;"
above.
$top->fileevent($pipe,"readable",[\&handlepipe,$pipe,$state]);
}
****************************************************
David Zempel
International Space Station Incubator Project
Lockheed Martin
ph. (408)742-1186
--Boundary_(ID_SN89RqUY92L6wO8WxD+/gg)
Content-type: text/html
Content-transfer-encoding: 7BIT
<html>
<head>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=us-ascii">
<meta name=Generator content="Microsoft Word 10 (filtered)">
<style>
<!--
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;
margin-bottom:.0001pt;
font-size:12.0pt;
font-family:"Times New Roman";}
a:link, span.MsoHyperlink
{color:blue;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{color:purple;
text-decoration:underline;}
p
{margin-right:0in;
margin-left:0in;
font-size:12.0pt;
font-family:"Times New Roman";}
span.EmailStyle17
{font-family:Arial;
color:windowtext;}
@page Section1
{size:8.5in 11.0in;
margin:1.0in 1.25in 1.0in 1.25in;}
div.Section1
{page:Section1;}
-->
</style>
</head>
<body lang=EN-US link=blue vlink=purple>
<div class=Section1>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'>I've been attempting to migrate some of my ptk tools
from unix to win32 and I've run into a bit of a problem. Several of my
tools execute external scripts via pipes. This all works great on unix using
methods discussed some time ago on this list. However, now that I'm
migrating some of these to win32 I'm running into an IO blocking problem.
I've posted this to the active state perl list with no successful
resolution. So I'm now posting to the ptk list. Has anybody been able to
establish non-blocking IO in perl on win32, and could you tell me how to
implement it?</span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'> </span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'>I'm including my current external command routine. It
works great on unix, but blocks until execution is complete on win32.</span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'> </span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'>Thanks, Dave.</span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'> </span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'>sub syscmd {</span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'> print "TRACE: syscmd\n" if ($trace == 1);</span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'> use FileHandle;</span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'> ($cmd,$state) = @_;</span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'> $pipe = new FileHandle; $|=1;</span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'> $pipe->open("$cmd|");</span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'># $pipe->autoflush(1); # this didn't
work anybetter than the "$|=1;" above.</span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'> $top->fileevent($pipe,"readable",[\&handlepipe,$pipe,$state]);</span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'>}</span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'> </span></font></p>
<p style='margin:0in;margin-bottom:.0001pt'><i><font size=2 color=teal
face="Times New Roman"><span style='font-size:10.0pt;color:teal;font-style:
italic'>****************************************************</span></font></i></p>
<p style='margin:0in;margin-bottom:.0001pt'><i><font size=2 color=teal
face="Times New Roman"><span style='font-size:10.0pt;color:teal;font-style:
italic'>David Zempel</span></font></i></p>
<p style='margin:0in;margin-bottom:.0001pt'><i><font size=2 color=teal
face="Times New Roman"><span style='font-size:10.0pt;color:teal;font-style:
italic'>International Space Station Incubator Project</span></font></i></p>
<p style='margin:0in;margin-bottom:.0001pt'><i><font size=2 color=teal
face="Times New Roman"><span style='font-size:10.0pt;color:teal;font-style:
italic'>Lockheed Martin</span></font></i></p>
<p style='margin:0in;margin-bottom:.0001pt'><i><font size=2 color=teal
face="Times New Roman"><span style='font-size:10.0pt;color:teal;font-style:
italic'>ph. (408)742-1186</span></font></i></p>
<p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size:
12.0pt'> </span></font></p>
</div>
</body>
</html>
--Boundary_(ID_SN89RqUY92L6wO8WxD+/gg)--
-++**==--++**==--++**==--++**==--++**==--++**==--++**==
This message was posted through the Stanford campus mailing list
server. If you wish to unsubscribe from this mailing list, send the
message body of "unsubscribe ptk" to majo...@lists.stanford.edu
> $top->fileevent($pipe,"readable",[\&handlepipe,$pipe,$state]);
>
I usually use a timer instead:
$top->repeat( 200, [\&handlepipe,$pipe,$state] );
Then use select() to determine whether there is really something to do or not.
That work for sockets. Other kinds of files may cause problems.
Torsten
sub syscmd {
use FileHandle;
($cmd,$state) = @_;
$pipe = new FileHandle;
$pipe->open("$cmd|");
$top->repeat(200,[\&handlepipe,$pipe,$state]);
}
sub handlepipe {
($pipe,$state) =@_;
$oldpipe = select( $pipe );
# $line = $pipe->getline();
$line = getline();
select( $oldpipe );
if (defined($line)) {
$formattedWindow->insert('end',$line);
sendCmd($line);
} else {
$oldpipe = select( $pipe );
$pipe->close();
# close();
select( $oldpipe );
config($state);
Oh, I didn't mean select(FILEHANDLE) I meant select( vector, vector, vector,
timeout ) or much simpler "use IO::Select; IO::Select->new( FILEHANDLE
)->can_read(0);":
use IO::Select; # untested
...
my $repeater;
$repeater=$top->repeat
( 200, sub {
1 while( my $rc=handlepipe( $pipe, $state ) );
$repeater->cancel if( defined $rc ); # $pipe has been closed
} );
...
sub handlepipe {
my ($pipe, $state)=@_;
# see whether some data can be read from $pipe immediately
if( IO::Select->new( $pipe )->can_read(0) ) {
# assumes the writer also writes whole lines at once.
# if that cannot be assured use $pipe->blocking(0) and
# $pipe->sysread(...);
my $line=$pipe->getline;
if( defined $line ) {
handleline( $line );
return 1;
} else {
$pipe->close;
return 0;
}
}
return;
}
Torsten