带有Azure Blob服务的Python

时间:2019-10-14 11:10:37

标签: python azure

我正在尝试使用Azure blob服务将视频文件上传到云。

我试图弄清楚如果我的互联网在传输过程中突然消失了会发生什么。

互联网出门时似乎没有异常。

from azure.common import AzureException
from azure.storage.blob import AppendBlobService, BlockBlobService, ContentSettings

    try:
        self.append_blob_service.append_blob_from_path(self.container_name, blob_name, upload_queue.get(timeout=3))
    except AzureException as ae:
        print("hey i caught something") <-- this line never seem to run

如果我将互联网重新放在Blob上,则似乎在30分钟后上传了自己。我在文档中找不到有关此的任何信息。 append_blob_from_path函数会持续尝试多长时间?

2 个答案:

答案 0 :(得分:0)

有LinearRetry,ExponentialRetry,NoRetry和自定义重试策略。

默认值为“线性”,最多间隔5秒5次。因此,如果您的网络连接断开时间少于25秒,您的上传将继续。

我不确定您的互联网连接是否断开了30分钟。在这种情况下,它应该抛出异常。

PS:您可以查找有关C#文档的重试策略。

答案 1 :(得分:0)

用于Azure存储的Python SDK是开源的:https://github.com/Azure/azure-storage-python

如果我们查看来自append_blob_from_path()的电话,就会看到以下内容:

  1. 有默认的套接字超时:
# Socket timeout in seconds
DEFAULT_SOCKET_TIMEOUT = 20
  1. 最后,它使用StorageClientAppendBlobService(BaseBlobService) -> BaseBlobService(StorageClient))中的函数,而StorageClient使用:
self.retry = ExponentialRetry().retry
  1. ExponentialRetry具有以下构造函数:
def __init__(self, initial_backoff=15, increment_base=3, max_attempts=3,
                 retry_to_secondary=False, random_jitter_range=3):
        '''
        Constructs an Exponential retry object. The initial_backoff is used for 
        the first retry. Subsequent retries are retried after initial_backoff + 
        increment_power^retry_count seconds. For example, by default the first retry 
        occurs after 15 seconds, the second after (15+3^1) = 18 seconds, and the 
        third after (15+3^2) = 24 seconds.
        :param int initial_backoff: 
            The initial backoff interval, in seconds, for the first retry.
        :param int increment_base:
            The base, in seconds, to increment the initial_backoff by after the 
            first retry.
        :param int max_attempts: 
            The maximum number of retry attempts.
        :param bool retry_to_secondary:
            Whether the request should be retried to secondary, if able. This should 
            only be enabled of RA-GRS accounts are used and potentially stale data 
            can be handled.
        :param int random_jitter_range:
            A number in seconds which indicates a range to jitter/randomize for the back-off interval.
            For example, a random_jitter_range of 3 results in the back-off interval x to vary between x+3 and x-3.
        '''
  1. 还有一个_retry() function使用RetryContext来决定是否需要重试

如果您在代码中启用INFO-level logging,则将看到所有重试:

# Basic configuration: configure the root logger, including 'azure.storage'
logging.basicConfig(format='%(asctime)s %(name)-20s %(levelname)-5s %(message)s', level=logging.INFO)

要恢复:

您有(20秒的套接字超时+动态间隔从15秒开始,每次尝试随机增加),并且有3次尝试。您可以看到在启用INFO级别的日志记录时到底发生了什么。