def read(self, length=None): """ Read a line from the socket if no length is specified, otherwise read ``length`` bytes. Always strip away the newlines. """ try: if length is not None: bytes_left = length + 2 # read the line ending if length > self.MAX_READ_LENGTH: # apparently reading more than 1MB or so from a windows # socket can cause MemoryErrors. See: # https://github.com/andymccurdy/redis-py/issues/205 # read smaller chunks at a time to work around this try: buf = BytesIO() while bytes_left > 0: read_len = min(bytes_left, self.MAX_READ_LENGTH) buf.write(self._fp.read(read_len)) bytes_left -= read_len buf.seek(0) return buf.read(length) finally: buf.close() return self._fp.read(bytes_left)[:-2] # no length, read a full line return self._fp.readline()[:-2] except (socket.error, socket.timeout): e = sys.exc_info()[1] raise ConnectionError("Error while reading from socket: %s" % (e.args, ))
def __init__(self, socket): self._sock = socket self._buffer = BytesIO() # number of bytes written to the buffer from the socket self.bytes_written = 0 # number of bytes read from the buffer self.bytes_read = 0
def __init__(self, socket, socket_read_size): self._sock = socket self.socket_read_size = socket_read_size self._buffer = BytesIO() # number of bytes written to the buffer from the socket self.bytes_written = 0 # number of bytes read from the buffer self.bytes_read = 0 self.unpacker = msgpack.Unpacker(None)
def pack_command(self, *args): "Pack a series of arguments into a value Redis command" output = BytesIO() output.write(SYM_STAR) output.write(b(str(len(args)))) output.write(SYM_CRLF) for enc_value in imap(self.encode, args): output.write(SYM_DOLLAR) output.write(b(str(len(enc_value)))) output.write(SYM_CRLF) output.write(enc_value) output.write(SYM_CRLF) return output.getvalue()