我们从Python开源项目中,提取了以下14个代码示例,用于说明如何使用io.BlockingIOError()。
def bind_unix_listener(self): # http://0pointer.de/blog/projects/systemd.html (search "file # descriptor 3") try: socket_fd = 3 self.sock = socket.fromfd(socket_fd, socket.AF_UNIX, socket.SOCK_STREAM) self.sock.listen(50) return self.sock except (TypeError, BlockingIOError, socket.error, ValueError): pass try: self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) if os.path.exists(MESSAGE_SOCK_PATH): os.remove(MESSAGE_SOCK_PATH) self.sock.bind(MESSAGE_SOCK_PATH) self.sock.listen(50) return self.sock except Exception as ex: exc_type, exc_value, exc_tb = sys.exc_info() traceback.print_exception(exc_type, exc_value, exc_tb, file=sys.stderr) raise ex
def _read_bytes(fp, size, error_template="ran out of data"): """ Read from file-like object until size bytes are read. Raises ValueError if not EOF is encountered before size bytes are read. Non-blocking objects only supported if they derive from io objects. Required as e.g. ZipExtFile in python 2.6 can return less data than requested. """ data = bytes() while True: # io files (default in python3) return None or raise on # would-block, python2 file will truncate, probably nothing can be # done about that. note that regular files can't be non-blocking try: r = fp.read(size - len(data)) data += r if len(r) == 0 or len(data) == size: break except io.BlockingIOError: pass if len(data) != size: msg = "EOF: reading %s, expected %d bytes got %d" raise ValueError(msg % (error_template, size, len(data))) else: return data
def run(self): self.bind_unix_listener() while not self._complete.is_set(): try: connection, client_address = self.sock.accept() self.read_socket(connection) except (TypeError, BlockingIOError, socket.error, ValueError): exc_type, exc_value, exc_tb = sys.exc_info() traceback.print_exception( exc_type, exc_value, exc_tb, file=sys.stderr) finally: connection.close()
def _flush_unlocked(self): self._checkClosed("flush of closed file") while self._write_buf: try: # ssl sockets only except 'bytes', not bytearrays # so perhaps we should conditionally wrap this for perf? n = self.raw.write(bytes(self._write_buf)) except io.BlockingIOError as e: n = e.characters_written del self._write_buf[:n]
def _read_bytes(fp, size, error_template="ran out of data"): """Read from file-like object until size bytes are read. Raises ValueError if not EOF is encountered before size bytes are read. Non-blocking objects only supported if they derive from io objects. Required as e.g. ZipExtFile in python 2.6 can return less data than requested. This function was taken from numpy/lib/format.py in version 1.10.2. Parameters ---------- fp: file-like object size: int error_template: str Returns ------- a bytes object The data read in bytes. """ data = bytes() while True: # io files (default in python3) return None or raise on # would-block, python2 file will truncate, probably nothing can be # done about that. note that regular files can't be non-blocking try: r = fp.read(size - len(data)) data += r if len(r) == 0 or len(data) == size: break except io.BlockingIOError: pass if len(data) != size: msg = "EOF: reading %s, expected %d bytes got %d" raise ValueError(msg % (error_template, size, len(data))) else: return data
def _flush_unlocked(self): self._checkClosed('flush of closed file') while self._write_buf: try: # ssl sockets only except 'bytes', not bytearrays # so perhaps we should conditionally wrap this for perf? n = self.raw.write(bytes(self._write_buf)) except io.BlockingIOError as e: n = e.characters_written del self._write_buf[:n]