Extends file_dispatcher to provide extra functionality for reading from and writing to pipes. Uses the observer pattern (recipe 576962) to provide notification of new data and closed pipes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 | #!/usr/bin/env python
"""asyncpipes.py: Asynchronous pipe communication using asyncore.
Extends `asyncore.file_dispatcher` to provide extra functionality for reading
from and writing to pipes. Uses the observer pattern to provide notification
of new data and closed pipes.
References:
http://code.activestate.com/recipes/576962/ [observer.py]
http://parijatmishra.blogspot.com/2008/01/writing-server-with-pythons-asyncore.html
"""
import os
from sys import stderr
from errno import EPIPE, EBADF
from asyncore import file_dispatcher
from traceback import print_exc
from observer import Observable
if __name__ == '__main__':
import optparse
from asyncore import loop
__version__ = '$Revision: 3742 $'.split()[1]
__usage__ = 'usage: %prog [options]'
class PipeDispatcher(Observable, file_dispatcher):
"""Dispatch pipe I/O using asyncore.
Allows synchronous access to the pipe by delegating to the filehandle,
though synchronous and asynchronous access should probably not be mixed.
"""
# Event sent when the pipe is closed
PIPE_CLOSED = 'closed'
# Default value for maximum pipe data
pipe_maxdata = 512
def __init__(self, fh, map=None, maxdata=None, ignore_broken_pipe=False, logger=None, **obsopt):
"""Wrap a dispatcher around the passed filehandle.
If `ignore_broken_pipe` is `True`, an `EPIPE` or `EBADF` error will
call `handle_close()` instead of `handle_expt()`. Useful when broken
pipes should be handled quietly.
`logger` is a logger which will be used to log unusual exceptions;
otherwise, they will be printed to stderr.
"""
self.maxdata = maxdata if maxdata else self.pipe_maxdata
self.__logger = logger
if ignore_broken_pipe:
self.__ignore_errno = [EPIPE, EBADF]
else:
self.__ignore_errno = []
self.__filehandle = fh
# Check for overduplication of the file descriptor and close the extra
fddup = os.dup(fh.fileno())
file_dispatcher.__init__(self, fddup, map=map)
if (self._fileno != fddup): os.close (fddup)
Observable.__init__(self, **obsopt)
def __getattr__(self, attr):
"""Delegate to the filehandle."""
return getattr(self.__filehandle, attr)
def close(self):
"""Close the pipe and calls the _obs_notify() method."""
if self.__filehandle:
try:
try:
file_dispatcher.close(self)
except OSError, oe:
if oe.errno not in self.__ignore_errno:
if self.__logger: self.__logger.exception("Unusual error closing pipe dispatcher")
else: print_exc(file=stderr)
try:
self.__filehandle.close()
except OSError, oe:
if oe.errno not in self.__ignore_errno:
if self.__logger: self.__logger.exception("Unusual error closing pipe filehandle")
else: print_exc(file=stderr)
finally:
self.__filehandle = None
self._obs_notify(event=self.PIPE_CLOSED)
def readable(self):
"""Return `True` if the pipe is still open."""
return (self.__filehandle is not None)
def writable(self):
"""Return `True` if the pipe is still open."""
return (self.__filehandle is not None)
def send(self, buffer):
"""Check for closed and broken pipes when sending data."""
if self.__filehandle:
try:
return file_dispatcher.send(self, buffer)
except OSError, oe:
if oe.errno in self.__ignore_errno: self.handle_close()
else: self.handle_expt()
return 0
def recv(self, buffer_size):
"""Check for closed and broken pipes when receiving data."""
if self.__filehandle:
try:
return file_dispatcher.recv(self, buffer_size)
except OSError, oe:
if oe.errno in self.__ignore_errno: self.handle_close()
else: self.handle_expt()
return ''
def handle_close(self):
"""Call `self.close()` to close the pipe."""
self.close()
def handle_expt(self):
"""Print a traceback and call `handle_close()` to close the pipe."""
if self.__logger: self.__logger.exception("Unusual exception in pipe I/O")
else: print_exc(file=stderr)
self.handle_close()
def _obs_exception(self):
"""Handle an exception raised by an observer."""
if self.__logger: self.__logger.exception("Unusual exception in pipe observer")
else: print_exc(file=stderr)
class InputPipeDispatcher(PipeDispatcher):
"""Push data to an input pipe using asyncore."""
def __init__(self, fh, close_when_done=False, **keywmap):
"""Wrap a dispatcher around the passed input filehandle.
`close_when_done` closes the pipe as soon as the buffer is empty after
the first `push_data()`. Useful for communicating with subprocesses
that read stdin to EOF before proceeding.
"""
self.__buffer = None
self.__offset = 0
self.__close_when_done = close_when_done
PipeDispatcher.__init__(self, fh, **keywmap)
def readable(self):
"""Return `False`; input pipes are never readable."""
return False
def writable(self):
"""Return `True` if data is in the buffer and the pipe is open."""
return PipeDispatcher.writable(self) and (self.__buffer is not None)
def handle_write(self):
"""Write up to `maxdata` bytes to the pipe."""
if self.writable():
self.__offset += self.send(
self.__buffer[self.__offset:self.__offset+self.maxdata])
# If the buffer is all written, empty it.
if self.__offset >= len(self.__buffer):
self.__buffer = None
self.__offset = 0
if self.__close_when_done: self.close()
def push_data(self, data):
"""Push some data by putting it in the write buffer.
Raise `EOFError` if the pipe is already closed.
"""
if not PipeDispatcher.writable(self):
raise EOFError('Input pipe closed.')
elif self.__buffer:
# Since we have to construct a new string, remove the already-sent data.
self.__buffer = self.__buffer[self.__offset:] + data
else:
self.__buffer = data
self.__offset = 0
class OutputPipeDispatcher(PipeDispatcher):
"""Get data from an output pipe using asyncore."""
PIPE_DATA = 'data'
"""Event sent when new data is available in the pipe."""
def __init__(self, fh, universal_newlines=False, **keywmap):
"""Wrap a dispatcher around the passed output filehandle.
`universal_newlines` converts all newlines found in the data stream to
'\n', just as in `subprocess.Popen`.
"""
self._universal_newlines = universal_newlines
self.__data = []
self.__endedcr = False
PipeDispatcher.__init__(self, fh, **keywmap)
def writable(self):
"""Return `False`; output pipes are never writable."""
return False
def handle_read(self):
"""Read and queue up to `maxdata` bytes, and notify any observers."""
if self.readable():
data = self.recv(self.maxdata)
if data:
self.__data.append(data)
self._obs_notify(self.PIPE_DATA)
def _translate_newlines(self, data):
data = data.replace("\r\n", "\n")
data = data.replace("\r", "\n")
return data
def fetch_data(self, clear=False):
"""Return all the accumulated data from the pipe as a string.
If `clear` is `True`, clear the accumulated data.
"""
if self.__data:
datastr = ''.join(self.__data)
if clear:
self.__data[:] = []
if datastr and self._universal_newlines:
# Take care of a newline split across cleared reads.
stripnl = self.__endedcr
if clear:
self.__endedcr = (datastr[-1] == '\r')
if stripnl and datastr[0] == '\n':
return self._translate_newlines(datastr[1:])
else:
return self._translate_newlines(datastr)
else:
return datastr
else:
return ''
def readlines(self, clear=False):
"""Return all complete lines from the pipe as a list of strings.
If `clear` is `True`, clear the accumulated data, but leave any
incomplete line
"""
datastr = self.fetch_data(clear)
lines = datastr.splitlines(True)
if lines[-1][-1] != '\n':
if clear:
self.__data[:] = [ lines[-1] ]
return lines[0:-1]
return lines
if __name__ == '__main__':
class TestAsyncPipe:
def __init__(self, maxprint, lineterm, loops, maxwrite, maxread):
self._maxprint = maxprint
self._lineterm = lineterm
self._loops = loops
rp, wp = os.pipe()
self._inpipe = InputPipeDispatcher(os.fdopen(wp, 'wb'),
maxdata=maxwrite)
self._outpipe = OutputPipeDispatcher(os.fdopen(rp, 'rb'),
maxdata=maxread, universal_newlines=(lineterm != '\n'))
self._inpipe.obs_add(self)
self._outpipe.obs_add(self)
def _printdata(self, data):
if not data:
printable = ''
elif len(data) > self._maxprint + 1:
printable = ': %r' % ('%s...%s' % (data[:self._maxprint], data[-1]))
else:
printable = ': %r' % data
print '%d bytes received%s' % (len(data), printable)
def handle_notify(self, pipe, event):
if event == OutputPipeDispatcher.PIPE_DATA:
data = pipe.fetch_data(clear=False)
self._printdata(data)
if data.endswith('\n'):
self._loops -= 1
if self._loops:
data = pipe.fetch_data(clear=True).strip()
self._inpipe.push_data(data + self._lineterm)
else:
self._inpipe.close()
self._outpipe.close()
else:
print '%s %s' % (pipe.__class__, event)
optparser = optparse.OptionParser(usage=__usage__, version=__version__)
optparser.disable_interspersed_args()
optparser.add_option('--data', default='0123456789',
help='Data string to be sent [%default]')
optparser.add_option('--copies', type=int, metavar='N', default=1,
help='Repeat the data N times (to test large transfers) [%default]')
optparser.add_option('--maxread', type='int', metavar='BYTES', default=1024,
help='Maximum data to read in each chunk [%default]')
optparser.add_option('--maxwrite', type='int', metavar='BYTES', default=1024,
help='Maximum data to write in each chunk [%default]')
optparser.add_option('--loops', type='int', metavar='N', default=5,
help='Number of loops to execute [%default]')
optparser.add_option('--lineterm', type='choice', metavar='TERM', choices=['CR','CRLF','LF'],
default='LF', help='Line terminator to send: CR, CRLF, or LF [%default]')
(options, args) = optparser.parse_args()
# Translate the line terminator to an escape sequence.
lineterm = {'CR':'\r', 'CRLF':'\r\n', 'LF':'\n'}[options.lineterm]
pipe_handler = TestAsyncPipe(len(options.data), lineterm, options.loops, options.maxwrite, options.maxread)
pipe_handler._inpipe.push_data(options.data * options.copies + lineterm)
loop()
|
Revision 8: Add some additional logging and exception handling. Revision 9: Add a method for reading complete lines from the pipe.