|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#说实话,我真不知道这玩意......
原文:
- Help on class BlockingIOError in module builtins:
- class BlockingIOError(OSError)
- | I/O operation would block.
- |
- | Method resolution order:
- | BlockingIOError
- | OSError
- | Exception
- | BaseException
- | object
- |
- | Methods defined here:
- |
- | __init__(self, /, *args, **kwargs)
- | Initialize self. See help(type(self)) for accurate signature.
- |
- | ----------------------------------------------------------------------
- | Methods inherited from OSError:
- |
- | __reduce__(self, /)
- | Helper for pickle.
- |
- | __str__(self, /)
- | Return str(self).
- |
- | ----------------------------------------------------------------------
- | Static methods inherited from OSError:
- |
- | __new__(*args, **kwargs) class method of builtins.OSError
- | Create and return a new object. See help(type) for accurate signature.
- |
- | ----------------------------------------------------------------------
- | Data descriptors inherited from OSError:
- |
- | characters_written
- |
- | errno
- | POSIX exception code
- |
- | filename
- | exception filename
- |
- | filename2
- | second exception filename
- |
- | strerror
- | exception strerror
- |
- | winerror
- | Win32 exception code
- |
- | ----------------------------------------------------------------------
- | Methods inherited from BaseException:
- |
- | __getattribute__(self, name, /)
- | Return getattr(self, name).
- |
- | __repr__(self, /)
- | Return repr(self).
- |
- | __setstate__(self, object, /)
- |
- | add_note(self, object, /)
- | Exception.add_note(note) --
- | add a note to the exception
- |
- | with_traceback(self, object, /)
- | Exception.with_traceback(tb) --
- | set self.__traceback__ to tb and return self.
- |
- | ----------------------------------------------------------------------
- | Data descriptors inherited from BaseException:
- |
- | __cause__
- | exception cause
- |
- | __context__
- | exception context
- |
- | __dict__
- |
- | __suppress_context__
- |
- | __traceback__
- |
- | args
复制代码
翻译:
- 模块内置类中 BlockingIOError 的帮助:
- 类 BlockingIOError(OSError)
- | I/O 操作将阻塞。
- |
- | 方法解析顺序:
- | 阻塞IOError
- | OSError
- | 异常
- | 基本异常
- | 对象
- |
- | 这里定义的方法:
- |
- | __init__(self, /, *args, **kwargs)
- | 初始化自身。 有关准确的签名,请参见 help(type(self)) 。
- |
- | ----------------------------------------------------------------------
- | 继承自 OSError 的方法:
- |
- | __reduce__(self, /)
- | 为 pickle 提供帮助。
- |
- | __str__(self, /)
- | 返回 str(self)。
- |
- | ----------------------------------------------------------------------
- | 继承自 OSError 的静态方法:
- |
- | buildins.OSError 的类方法 __new__(*args, **kwargs)
- | 创建并返回一个新对象。 请参见 help(type) 获取准确的签名。
- |
- | ----------------------------------------------------------------------
- | 从 OSError 继承的数据描述符:
- |
- | 字符写入
- |
- | Erno
- | POSIX 异常代码
- |
- | 文件名
- | 异常文件名
- |
- | 文件名2
- | 第二个异常文件名
- |
- | stringerror
- | 异常 strerror
- |
- | winerror
- | Win32 异常代码
- |
- | ----------------------------------------------------------------------
- | 继承自 BaseException 的方法:
- |
- | __getattribute__(self, name, /)
- | 返回 getattr(self,name)。
- |
- | __repr__(self, /)
- | 返回 repr(self)。
- |
- | 返回 __setstate__(self, object, /)
- |
- | add_note(self, object, /)
- | Exception.add_note(note) --
- | 为异常添加注释
- |
- | with_traceback(self, object, /)
- | Exception.with_traceback(tb) -- | 为异常添加注释。
- | 将 self.__traceback__ 设为 tb 并返回 self。
- |
- | ----------------------------------------------------------------------
- | 从 BaseException 继承的数据描述符:
- |
- | __cause__
- | 异常原因
- |
- | __context__
- | 异常上下文
- |
- | __dict__
- |
- | __suppress_context__
- |
- | __traceback__
- |
- | args
复制代码
#翻译以后就机翻好了, 因为太过于零碎......本人英语也不太好
捣鼓了好一阵,开了个Deepl Pro...希望大家支持一下
好了废话不多说,看用法:
Raised when an operation would block on an object (e.g. socket) set for non-blocking operation. Corresponds to EAGAIN, EALREADY, EWOULDBLOCK and EINPROGRESS.errno
什么意思呢?当操作会阻塞为非阻塞操作设置的对象(如套接字)时发生。与 EAGAIN、EALREADY、EWOULDBLOCK 和 EINPROGRESS.errno 对应。
我们来慢慢品读一下:
首先, 非堵塞操作是指当一个操作(如读取数据、发送数据或建立连接)无法立即完成时,程序不会等待操作完成,而是立即返回并报告一个错误或特定的状态,表明操作尚未完成。
也就是说函数调用立即返回,无论操作是否完成,程序可以继续执行其他任务。
套接字呢, 就是指如果操作无法立即完成,套接字会返回一个错误(如EAGAIN或EWOULDBLOCK),表明当前没有可用的数据或资源。
示例:
- import socket
- # 创建一个非阻塞套接字
- non_blocking_socket = socket.socket(socket.AF_INET,
- socket.SOCK_STREAM)
- non_blocking_socket.setblocking(False)
- # 尝试连接到服务器
- try:
- non_blocking_socket.connect(('example.com', 80))
- except BlockingIOError:
- print("连接操作会阻塞")
- # 可能在此处执行其他任务,不需要等待连接完成
- # 接收数据(非阻塞方式)
- try:
- data = non_blocking_socket.recv(1024)
- except BlockingIOError:
- print("没有数据可接收")
- # 可能在此处执行其他任务,不需要等待数据到达
- # 关闭套接字连接
- non_blocking_socket.close()
复制代码
#事实上, 在网上搜了好多资料, 不知道这个能干啥, 求指点 |
|