鱼C论坛

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

[技术交流] BrokenPipeError--PythonBIF(7)

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

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

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

x
原文:

  1. Help on class BrokenPipeError in module builtins:

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

翻译:

  1. 模块内置程序中 BrokenPipeError 类的帮助:

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

不要以为是机翻错了, 他事实上就叫管道破裂.

在操作系统中,进程间通信经常使用管道。管道是一种单向通信机制,用于将一个进程的输出连接到另一个进程的输入。当管道的读取端关闭时,对于向其写入数据的进程而言,它是一个破损的管道。当一个进程向一个破损的管道写入数据时,操作系统会发送一个SIGPIPE信号给该进程,通知它管道已经破损。在Python中,这个SIGPIPE信号会被转化为BrokenPipeError异常。


当你的Python程序尝试将输出重定向到一个管道(通常是日志文件)时,就会发生“Broken Pipe”错误。管道就像一条虚拟的管道,允许数据从一个进程传输到另一个进程。如果这个管道断开,就会触发这个错误。

怎么说呢, 它的发生需要一定条件.主线程意外终止: 负责打印输出的主线程意外终止,子进程终止,日志文件不可写,管道缓冲区已满

使用try...except...可限制资源使用,以防止主线程意外终止。

示例:

  1. import logging

  2. # 设置日志记录
  3. logging.basicConfig(filename='mylog.log', level=logging.DEBUG)

  4. # 创建主线程
  5. def main_thread():
  6.     try:
  7.         # ... 程序代码 ...

  8.         # 写入日志
  9.         logging.info('主线程输出')
  10.     except Exception as e:
  11.         # 处理异常
  12.         logging.error(e)

  13. # 启动主线程
  14. main_thread()
复制代码

通过使用“try”和“except”语句来处理异常,我们防止了主线程意外终止。此外,我们使用“logging”模块进行日志记录,而不是直接重定向到管道。

#不太常用,反正我没见过
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2025-1-27 19:58:24 | 显示全部楼层
BrokenPipeError--Python 内置函数帮助文档

原文:

Help on class BrokenPipeError in module builtins:


  1. class BrokenPipeError(ConnectionError)
  2. |  Broken pipe.
  3. |
  4. |  Method resolution order:
  5. |      BrokenPipeError
  6. |      ConnectionError
  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
复制代码


翻译:

模块内置程序中 BrokenPipeError 类的帮助:


  1. 类 BrokenPipeError(ConnectionError)
  2. | 管道破裂。
  3. |
  4. | 方法解析顺序:
  5. |      BrokenPipeError
  6. |      ConnectionError
  7. |      OSError
  8. |      Exception
  9. |      BaseException
  10. |      object
  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. |  __new__(*args, **kwargs) class method of builtins.OSError
  30. |      创建并返回一个新对象。 请参见 help(type) 获取准确的签名。
  31. |
  32. | ----------------------------------------------------------------------
  33. | 从 OSError 继承的数据描述符:
  34. |
  35. |  characters_written
  36. |
  37. |  errno
  38. |      POSIX 异常代码
  39. |
  40. |  filename
  41. |      异常文件名
  42. |
  43. |  filename2
  44. |      第二个异常文件名
  45. |
  46. |  strerror
  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. |  with_traceback(self, object, /)
  67. |      Exception.with_traceback(tb) -- 将 self.__traceback__ 设为 tb 并返回 self。
  68. |
  69. | ----------------------------------------------------------------------
  70. | 从 BaseException 继承的数据描述符:
  71. |
  72. |  __cause__
  73. |      异常原因
  74. |
  75. |  __context__
  76. |      异常上下文
  77. |
  78. |  __dict__
  79. |
  80. |  __suppress_context__
  81. |
  82. |  __traceback__
  83. |
  84. |  args
复制代码


解释:

在操作系统中,进程间通信经常使用管道。管道是一种单向通信机制,用于将一个进程的输出连接到另一个进程的输入。当管道的读取端关闭时,对于向其写入数据的进程而言,它是一个破损的管道。当一个进程向一个破损的管道写入数据时,操作系统会发送一个 SIGPIPE 信号给该进程,通知它管道已经破损。在Python中,这个 SIGPIPE 信号会被转化为 BrokenPipeError 异常。

当你的Python程序尝试将输出重定向到一个管道(通常是日志文件)时,就会发生“Broken Pipe”错误。

使用示例:

通过使用 tryexcept 语句来处理异常,我们可以防止主线程意外终止。此外,我们使用 logging 模块进行日志记录,而不是直接重定向到管道。


  1. import logging

  2. # 设置日志记录
  3. logging.basicConfig(filename='mylog.log', level=logging.DEBUG)

  4. # 创建主线程
  5. def main_thread():
  6.     try:
  7.         # ... 程序代码 ...

  8.         # 写入日志
  9.         logging.info('主线程输出')
  10.     except Exception as e:
  11.         # 处理异常
  12.         logging.error(e)

  13. # 启动主线程
  14. main_thread()
复制代码


希望这能帮到你!

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-5-6 03:00

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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