Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

blocking IO on win32 question

5 views
Skip to first unread message

Zempel, David

unread,
Nov 15, 2002, 5:17:13 PM11/15/02
to
This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.

--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'>&nbsp;</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'>&nbsp;</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'>&nbsp;</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'>&nbsp; print &quot;TRACE: syscmd\n&quot; if ($trace == 1);</span></font></p>

<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'>&nbsp; use FileHandle;</span></font></p>

<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'>&nbsp; ($cmd,$state) = @_;</span></font></p>

<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'>&nbsp; $pipe = new FileHandle; $|=1;</span></font></p>

<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'>&nbsp; $pipe-&gt;open(&quot;$cmd|&quot;);</span></font></p>

<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'>#&nbsp; $pipe-&gt;autoflush(1);&nbsp; # 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'>&nbsp; $top-&gt;fileevent($pipe,&quot;readable&quot;,[\&amp;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'>&nbsp;</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'>&nbsp;</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

Torsten Foertsch

unread,
Nov 17, 2002, 12:15:53 PM11/17/02
to
On Friday 15 November 2002 22:08, Zempel, David wrote:
> 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?
>
fileevents do not work on WIN32.

> $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

Zempel, David

unread,
Nov 18, 2002, 2:26:17 PM11/18/02
to
Torsten,
Thanks. I made the change however, I'm not there quite yet. What perl
function/method do you use to capture the input from the pipe? I tried to
continue to use getline since the data is "record" oriented. But I'm getting
a flurry of Filehandle errors. I'm doing a select before and after every IO
to the pipe to set and restore my current default output channel. Any
ideas? Thanks, Dave.


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);

Torsten Foertsch

unread,
Nov 19, 2002, 5:21:34 AM11/19/02
to

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

0 new messages