鱼C论坛

 找回密码
 立即注册
查看: 284|回复: 1

[技术交流] BlockingIOError--PythonBIF(6)

[复制链接]
发表于 2025-1-27 19:46:06 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#说实话,我真不知道这玩意......
原文:

  1. Help on class BlockingIOError in module builtins:

  2. class BlockingIOError(OSError)
  3. |  I/O operation would block.
  4. |
  5. |  Method resolution order:
  6. |      BlockingIOError
  7. |      OSError
  8. |      Exception
  9. |      BaseException
  10. |      object
  11. |
  12. |  Methods defined here:
  13. |
  14. |  __init__(self, /, *args, **kwargs)
  15. |      Initialize self.  See help(type(self)) for accurate signature.
  16. |
  17. |  ----------------------------------------------------------------------
  18. |  Methods inherited from OSError:
  19. |
  20. |  __reduce__(self, /)
  21. |      Helper for pickle.
  22. |
  23. |  __str__(self, /)
  24. |      Return str(self).
  25. |
  26. |  ----------------------------------------------------------------------
  27. |  Static methods inherited from OSError:
  28. |
  29. |  __new__(*args, **kwargs) class method of builtins.OSError
  30. |      Create and return a new object.  See help(type) for accurate signature.
  31. |
  32. |  ----------------------------------------------------------------------
  33. |  Data descriptors inherited from OSError:
  34. |
  35. |  characters_written
  36. |
  37. |  errno
  38. |      POSIX exception code
  39. |
  40. |  filename
  41. |      exception filename
  42. |
  43. |  filename2
  44. |      second exception filename
  45. |
  46. |  strerror
  47. |      exception strerror
  48. |
  49. |  winerror
  50. |      Win32 exception code
  51. |
  52. |  ----------------------------------------------------------------------
  53. |  Methods inherited from BaseException:
  54. |
  55. |  __getattribute__(self, name, /)
  56. |      Return getattr(self, name).
  57. |
  58. |  __repr__(self, /)
  59. |      Return repr(self).
  60. |
  61. |  __setstate__(self, object, /)
  62. |
  63. |  add_note(self, object, /)
  64. |      Exception.add_note(note) --
  65. |      add a note to the exception
  66. |
  67. |  with_traceback(self, object, /)
  68. |      Exception.with_traceback(tb) --
  69. |      set self.__traceback__ to tb and return self.
  70. |
  71. |  ----------------------------------------------------------------------
  72. |  Data descriptors inherited from BaseException:
  73. |
  74. |  __cause__
  75. |      exception cause
  76. |
  77. |  __context__
  78. |      exception context
  79. |
  80. |  __dict__
  81. |
  82. |  __suppress_context__
  83. |
  84. |  __traceback__
  85. |
  86. |  args
复制代码

翻译:

  1. 模块内置类中 BlockingIOError 的帮助:

  2. 类 BlockingIOError(OSError)
  3. | I/O 操作将阻塞。
  4. |
  5. | 方法解析顺序:
  6. | 阻塞IOError
  7. | OSError
  8. | 异常
  9. | 基本异常
  10. | 对象
  11. |
  12. | 这里定义的方法:
  13. |
  14. | __init__(self, /, *args, **kwargs)
  15. | 初始化自身。 有关准确的签名,请参见 help(type(self)) 。
  16. |
  17. | ----------------------------------------------------------------------
  18. | 继承自 OSError 的方法:
  19. |
  20. | __reduce__(self, /)
  21. | 为 pickle 提供帮助。
  22. |
  23. | __str__(self, /)
  24. | 返回 str(self)。
  25. |
  26. | ----------------------------------------------------------------------
  27. | 继承自 OSError 的静态方法:
  28. |
  29. | buildins.OSError 的类方法 __new__(*args, **kwargs)
  30. | 创建并返回一个新对象。 请参见 help(type) 获取准确的签名。
  31. |
  32. | ----------------------------------------------------------------------
  33. | 从 OSError 继承的数据描述符:
  34. |
  35. | 字符写入
  36. |
  37. | Erno
  38. | POSIX 异常代码
  39. |
  40. | 文件名
  41. | 异常文件名
  42. |
  43. | 文件名2
  44. | 第二个异常文件名
  45. |
  46. | stringerror
  47. | 异常 strerror
  48. |
  49. | winerror
  50. | Win32 异常代码
  51. |
  52. | ----------------------------------------------------------------------
  53. | 继承自 BaseException 的方法:
  54. |
  55. | __getattribute__(self, name, /)
  56. | 返回 getattr(self,name)。
  57. |
  58. | __repr__(self, /)
  59. | 返回 repr(self)。
  60. |
  61. | 返回 __setstate__(self, object, /)
  62. |
  63. | add_note(self, object, /)
  64. | Exception.add_note(note) --
  65. | 为异常添加注释
  66. |
  67. | with_traceback(self, object, /)
  68. | Exception.with_traceback(tb) -- | 为异常添加注释。
  69. | 将 self.__traceback__ 设为 tb 并返回 self。
  70. |
  71. | ----------------------------------------------------------------------
  72. | 从 BaseException 继承的数据描述符:
  73. |
  74. | __cause__
  75. | 异常原因
  76. |
  77. | __context__
  78. | 异常上下文
  79. |
  80. | __dict__
  81. |
  82. | __suppress_context__
  83. |
  84. | __traceback__
  85. |
  86. | 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),表明当前没有可用的数据或资源。

示例:

  1. import socket

  2. # 创建一个非阻塞套接字
  3. non_blocking_socket = socket.socket(socket.AF_INET,
  4.                                     socket.SOCK_STREAM)
  5. non_blocking_socket.setblocking(False)

  6. # 尝试连接到服务器
  7. try:
  8.     non_blocking_socket.connect(('example.com', 80))
  9. except BlockingIOError:
  10.     print("连接操作会阻塞")

  11. # 可能在此处执行其他任务,不需要等待连接完成

  12. # 接收数据(非阻塞方式)
  13. try:
  14.     data = non_blocking_socket.recv(1024)
  15. except BlockingIOError:
  16.     print("没有数据可接收")

  17. # 可能在此处执行其他任务,不需要等待数据到达

  18. # 关闭套接字连接
  19. non_blocking_socket.close()
复制代码

#事实上, 在网上搜了好多资料, 不知道这个能干啥, 求指点
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2025-1-27 19:46:18 | 显示全部楼层
BlockingIOError 在 Python 中的使用

BlockingIOError 是一个内置的异常类,继承自 OSError。它在执行 I/O 操作时,如果一个操作试图访问一个会阻塞的非阻塞对象(如非阻塞套接字)时被抛出。

非阻塞操作意味着函数调用会立即返回,而不等待操作完成。对于套接字(socket),这非常有用,因为它允许程序继续执行其他任务,而不是等到数据传输完成。

示例代码:

  1. import socket

  2. # 创建一个非阻塞套接字
  3. non_blocking_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  4. non_blocking_socket.setblocking(False)

  5. # 尝试连接到服务器
  6. try:
  7.     non_blocking_socket.connect(('example.com', 80))
  8. except BlockingIOError:
  9.     print("连接操作会阻塞")

  10. # 可能在此处执行其他任务,不需要等待连接完成

  11. # 接收数据(非阻塞方式)
  12. try:
  13.     data = non_blocking_socket.recv(1024)
  14. except BlockingIOError:
  15.     print("没有数据可接收")

  16. # 可能在此处执行其他任务,不需要等待数据到达

  17. # 关闭套接字连接
  18. non_blocking_socket.close()
复制代码


在上述示例代码中:
1. 我们创建了一个非阻塞套接字,并尝试连接到服务器。
2. 如果连接操作会阻塞,它将引发 BlockingIOError 异常,并打印相应的消息。
3. 程序可以继续执行其他任务,而无需等待连接或数据传输完成。

这种方式在需要高效处理大量并发连接的场景下特别有用,例如网络服务器或客户端程序。

希望这个解释能够帮助你理解 BlockingIOError 的用途!如果有任何进一步的问题,请随时提出。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-6 02:38

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表