【问题标题】:Python Azure module error handling TCP 104 not being caughtPython Azure 模块错误处理 TCP 104 未被捕获
【发布时间】:2022-01-25 02:24:07
【问题描述】:

我目前正在使用带有 azure.storage.blob 12.9.0 版和 azure.core 1.14.0 版的 python 3.8.8。

我正在使用 azure.storage.blob 包下载多个文件。我的代码如下所示

from azure.storage.blob import ContainerClient
from azure.core.exceptions import ResourceNotFoundError, AzureError
from time import sleep

max_attempts = 5
container_client = ContainerClient(DETAILS)

for file in multiple_files:

  attempts = 0

  while attempts < max_attempts:
  
    try:
    
      data = container.download_blob(file).readall()
      break
      
    except ResourceNotFoundError:
      # log missing data
      break
      
    except AzureError:
      # This is mainly here as connections seem to drop randomly.
      attempts += 1
      sleep(1)
      
  if attempts >= max_attempts:
    #log connection error
      
  #do something with the data.

它似乎运行良好,我没有看到任何数据丢失。但是,在我的终端中,我不断收到消息

Unable to stream download: ("Connection broken: ConnectionResetError(104, 'Connection reset by peer')", ConnectionResetError(104, 'Connection reset by peer'))

这似乎是一条 TCP 104 返回消息,但未由 azure 模块处理。我的问题如下。

  1. 此消息来自哪里?我在使用的任何软件包中都看不到它。
  2. 如何更好地处理此错误?它似乎没有被捕获为异常,因为它没有使我的代码崩溃。
  3. 我可以将此打印到日志中吗?

【问题讨论】:

    标签: python azure error-handling tcp azure-blob-storage


    【解决方案1】:
    1. 此消息来自哪里?我在使用的任何软件包中都看不到它。

    看起来客户端似乎已连接到服务器,但是当他们尝试传输数据时,他们收到了 Errno 104 Connection reset by peer 错误。这也意味着,另一端已重置连接,否则客户端将遇到[Errno 32] Broken pipe 异常。


    1. 如何更好地处理此错误?它似乎没有被捕获为异常,因为它没有使我的代码崩溃。

    您可以尝试的一种解决方法是使用 try 和 catch 块来处理该异常:

    from socket import error as SocketError
    import errno
    
    try:
        response = urllib2.urlopen(request).read()
    except SocketError as e:
        if e.errno != errno.ECONNRESET:
            raise # Not error we are looking for
        pass # Handle error here.
    

    也可以尝试参考这个similar issue sudo pip3 install urllib3 解决了这个问题。


    1. 我可以将此打印到日志中吗?

    一种解决方法是您可以在 exc_info 参数中传递异常实例:

    import logging
    try:
        1/0
    except Exception as e:
       logging.error('Error at %s', 'division', exc_info=e)
    

    更多信息可以参考How to log python exception?

    这是一个相关问题,您可以跟进 azure storage blob download: ConnectionResetError(104, 'Connection reset by peer')

    参考: Connection broken: ConnectionResetError(104, 'Connection reset by peer') error while streaming

    【讨论】:

    • 感谢您的回答,安装 urllib3 有帮助,但我还在 except 语句中添加了更多安全性。
    猜你喜欢
    • 2019-05-19
    • 1970-01-01
    • 2014-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-23
    相关资源
    最近更新 更多