ch07_14.htm
来自「By Tom Christiansen and Nathan Torkingto」· HTM 代码 · 共 442 行
HTM
442 行
<HTML><HEAD><TITLE>Recipe 7.13. Reading from Many Filehandles Without Blocking (Perl Cookbook)</TITLE><METANAME="DC.title"CONTENT="Perl Cookbook"><METANAME="DC.creator"CONTENT="Tom Christiansen & Nathan Torkington"><METANAME="DC.publisher"CONTENT="O'Reilly & Associates, Inc."><METANAME="DC.date"CONTENT="1999-07-02T01:37:14Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-243-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch07_01.htm"TITLE="7. File Access"><LINKREL="prev"HREF="ch07_13.htm"TITLE="7.12. Flushing Output"><LINKREL="next"HREF="ch07_15.htm"TITLE="7.14. Doing Non-Blocking I/O"></HEAD><BODYBGCOLOR="#FFFFFF"><img alt="Book Home" border="0" src="gifs/smbanner.gif" usemap="#banner-map" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Perl Cookbook"><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><p><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch07_13.htm"TITLE="7.12. Flushing Output"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 7.12. Flushing Output"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"><ACLASS="chapter"REL="up"HREF="ch07_01.htm"TITLE="7. File Access"></A></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch07_15.htm"TITLE="7.14. Doing Non-Blocking I/O"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 7.14. Doing Non-Blocking I/O"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch07-18871">7.13. Reading from Many Filehandles Without Blocking</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch07-pgfId-1304">Problem<ACLASS="indexterm"NAME="ch07-idx-1000009716-0"></A><ACLASS="indexterm"NAME="ch07-idx-1000009716-1"></A><ACLASS="indexterm"NAME="ch07-idx-1000009716-2"></A><ACLASS="indexterm"NAME="ch07-idx-1000009716-3"></A><ACLASS="indexterm"NAME="ch07-idx-1000009716-4"></A></A></H3><PCLASS="para">You want to learn whether input is available to be read, rather than blocking for input as < > does. This is useful when reading from pipes, sockets, devices, and other programs.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch07-pgfId-1310">Solution</A></H3><PCLASS="para">Use <CODECLASS="literal">select</CODE> with a timeout value of 0 seconds, if you're comfortable with manipulating bit-vectors representing file descriptor sets:</P><PRECLASS="programlisting">$rin = '';# repeat next line for all filehandles to pollvec($rin, fileno(FH1), 1) = 1;vec($rin, fileno(FH2), 1) = 1;vec($rin, fileno(FH3), 1) = 1;$nfound = select($rout=$rin, undef, undef, 0);if ($nfound) { # input waiting on one or more of those 3 filehandles if (vec($rout,fileno(FH1),1)) { # do something with FH1 } if (vec($rout,fileno(FH2),1)) { # do something with FH2 } if (vec($rout,fileno(FH3),1)) { # do something with FH3 }}</PRE><PCLASS="para">The IO::Select module provides an abstraction to hide the bit-vector operations:</P><PRECLASS="programlisting">use IO::Select;$select = IO::Select->new();# repeat next line for all filehandles to poll$select->add(*FILEHANDLE);if (@ready = $select->can_read(0)) { # input waiting on the filehandles in @ready}</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch07-pgfId-1370">Discussion</A></H3><PCLASS="para">The <CODECLASS="literal">select</CODE> function is really two functions in one. If you call it with one argument, you change the current default output filehandle (see <ACLASS="xref"HREF="ch07_13.htm"TITLE="Flushing Output">Recipe 7.12</A>). If you call it with four arguments, it tells you which filehandles have input waiting or are ready to receive output. This recipe only deals with four-argument <CODECLASS="literal">select</CODE>.</P><PCLASS="para">The first three arguments to <CODECLASS="literal">select</CODE> are strings containing bit-vectors. Each bit-vector represents a set of file descriptors to inspect for pending input, pending output, and pending expedited data (like out-of-band or urgent data on a socket), respectively. The final argument is the timeout - how long <CODECLASS="literal">select</CODE> should spend waiting for status to change. A timeout value of 0 indicates a poll. Timeout can also be a floating-point number of seconds, or <CODECLASS="literal">undef</CODE> to wait (block) until status changes:</P><PRECLASS="programlisting">$rin = '';vec($rin, fileno(FILEHANDLE), 1) = 1;$nfound = select($rin, undef, undef, 0); # just checkif ($nfound) { $line = <FILEHANDLE>; print "I read $line";}</PRE><PCLASS="para">This code isn't perfect, though. If someone connects and sends a character but never sends a newline, your program will block in the <CODECLASS="literal"><FILE></CODE>. We get around this by reading characters one at a time and processing completed lines when we read a <CODECLASS="literal">"\n"</CODE>. This removes the need for the blocking <CODECLASS="literal"><FILE></CODE> call. Another solution (if you're not testing files) is detailed in <ACLASS="xref"HREF="ch07_16.htm"TITLE="Determining the Number of Bytes to Read">Recipe 7.15</A>.</P><PCLASS="para">The IO::Select module hides the bit-vectors from you. <CODECLASS="literal">IO::Select->new()</CODE> returns a new object on which you call the <CODECLASS="literal">add</CODE> method to add one or more filehandles to the set. When you've added all the filehandles you are interested in, call <CODECLASS="literal">can_read</CODE>, <CODECLASS="literal">can_write</CODE>, or <CODECLASS="literal">has_exception</CODE>. These functions return a list of filehandles you can safely read from, write to, or that have unread exceptional data (TCP out-of-band data, for example).</P><PCLASS="para">Don't mix calls to four-argument <CODECLASS="literal">select</CODE> with calls to any of the buffered I/O functions listed in this chapter's Introduction (<CODECLASS="literal">read</CODE>, <>, <CODECLASS="literal">seek</CODE>, <CODECLASS="literal">tell</CODE>, etc.). Instead, you must use <CODECLASS="literal">sysread </CODE> - and <CODECLASS="literal">sysseek</CODE> if you want to reposition the filehandle within the file.</P><PCLASS="para">If you want to read whatever is available on a socket or pipe and return immediately, see Recipe 7.14. If you're trying to do non-blocking reads on a terminal, see Recipes <ACLASS="xref"HREF="ch15_07.htm"TITLE="Reading from the Keyboard">Recipe 15.6</A> and <ACLASS="xref"HREF="ch15_09.htm"TITLE="Using POSIX termios">Recipe 15.8</A>. <ACLASS="indexterm"NAME="ch07-idx-1000009718-0"></A><ACLASS="indexterm"NAME="ch07-idx-1000009718-1"></A><ACLASS="indexterm"NAME="ch07-idx-1000009718-2"></A><ACLASS="indexterm"NAME="ch07-idx-1000009718-3"></A><ACLASS="indexterm"NAME="ch07-idx-1000009718-4"></A></P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch07-pgfId-1400">See Also</A></H3><PCLASS="para">The <CODECLASS="literal">select</CODE> function in <ICLASS="filename">perlfunc </I>(1) and in <ACLASS="olink"HREF="../prog/ch03_01.htm">Chapter 3</A> of <ACLASS="citetitle"HREF="../prog/index.htm"TITLE="Programming Perl"><CITECLASS="citetitle">Programming Perl</CITE></A>; the documentation for the standard module IO::Select; <ACLASS="xref"HREF="ch07_15.htm"TITLE="Doing Non-Blocking I/O">Recipe 7.14</A></P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch07_13.htm"TITLE="7.12. Flushing Output"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 7.12. Flushing Output"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="book"HREF="index.htm"TITLE="Perl Cookbook"><IMGSRC="../gifs/txthome.gif"ALT="Perl Cookbook"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch07_15.htm"TITLE="7.14. Doing Non-Blocking I/O"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 7.14. Doing Non-Blocking I/O"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">7.12. Flushing Output</TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="index"HREF="index/index.htm"TITLE="Book Index"><IMGSRC="../gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228">7.14. Doing Non-Blocking I/O</TD></TR></TABLE><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><FONTSIZE="-1"></DIV<!-- LIBRARY NAV BAR --> <img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p> <a href="copyrght.htm">Copyright © 2002</a> O'Reilly & Associates. All rights reserved.</font> </p> <map name="library-map"> <area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map> </BODY></HTML>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?