4. Asynchronous Files and Pipes (asyncfile)¶
asyncfile module provides API for asynchronous files and pipes to
be used in tasks.
4.1. Examples¶
Following is a brief description of the examples included relevant to this section (unlike examples in other sections which tend to be a bit trivial, these examples are more complicated than necessary, to introduce different features / approaches):
examples/pipe_csum.pyuses asynchronous pipes to write data to and read data from a system program (that computes checksum of data).examples/pipe_grep.pyuses chained pipes with asynchronous read and write interface to count number of lines matching a pattern.
4.2. Asynchronous File¶
AsyncFile class wraps given file so it can be used for
asynchronous I/O. Note that regular on-disk files are non-blocking and
can’t be used for polling and asynchronous I/O under Linux, OS X and
other Unix variants. Under Windows asynchronous I/O of on-disk files
is supported, but it may not be very useful. However, under Linux, OS
X and other Unix variants, network sockets can be converted to
asynchronous file interface so read(), write(),
readline() in AsyncFile can be used. With Windows,
though, network sockets don’t use file in implementation so sockets
can’t be converted to AsyncFile under Windows.
AsyncFile is used in implementing AsyncPipe.
-
class
asyncfile.AsyncFile(fd)¶ Note
This interface is for Linux, OS X and other Unix variants.
Sets up given file descriptor (that was created with
open()orsocket.socket) for asynchronous I/O.
4.2.1. Examples¶
See socket_afile.py in the examples directory.
4.3. Asynchronous Pipe¶
AsyncPipe provides asynchronous API for pipes.
Under Windows, Popen in asyncfile module must be used instead
of subprocess.Popen.
-
class
asyncfile.AsyncPipe(first, last=None)¶ Sets up (chained) pipe for asynchronous I/O. first must be
subprocess.Popenobject under Linux, OS X and other Unix variants andasyncfile.Popenobject under Windows. If last isNone, the pipe is not chained and in the description below, last is same as first; otherwise, last must also be aPopenobject (representing end of chained pipe).write()operations send data to first's stdin andread()operations get data from last's stdout/stderr.Asynchronous pipes support following methods:
-
write(buf, full=False, timeout=None)¶ Note
This method must be used with yield as
n = yield apipe.write(buf)Writes data in buf to stdin of first. full, timeout and operation of this method are same as that for
write()ofAsyncFile.
-
read(size=0, timeout=None)¶ Note
This method must be used with yield as
buf = yield apipe.read()Reads data from stdout of last. timeout and operation of this method are same as that for
read()ofAsyncFile.
-
readline(size=0, sizehint=100, timeout=None)¶ Note
This method must be used with yield as
line = yield apipe.readline()Reads a line from stdout of last. sizehint, timeout and operation of this method are same as that for
readline()ofAsyncFile.
-
read_stderr(size=0, timeout=None)¶ Note
This method must be used with yield as
buf = yield apipe.read_stderr()Reads data from stderr of last. timeout and operation of this method are same as that for
read()ofAsyncFile.
-
readline_stderr(size=0, sizehint=100, timeout=None)¶ Note
This method must be used with yield as
line = yield apipe.readline_stderr()Reads a line from stderr of last. sizehint, timeout and operation of this method are same as that for
readline()ofAsyncFile.
-
communicate(input=None)¶ Note
This method must be used with yield as
stdoutdata, stderrdata = yield apipe.communicate()Similar to
Popen.communicate, except that input can be either data (as perPopen.communicate) or a file descriptor. The file descriptor can be synchronous (obtained withopen) or asynchronous (created withAsyncFile).
-
close()¶ Closes pipe.
-