如何使用ETag更新Azure表存储实体

2024-03-28 08:41:08 发布

您现在位置:Python中文网/ 问答频道 /正文

概述:当我在blob存储中的container/productID(folder)/blobName下上载blob时,事件订阅会将此事件保存在存储队列中。之后,azure函数轮询此事件并执行以下操作:

1- read from the corresponding table the current count property (how many blobs are stored under productID(folder)) with the etag

2- increase the count + 1

3- write it back in the corresponding table, if ETag is matched, then the field count will be increased, otherwise throws an error. if err is thrown, sleep while and go to step 1 (while loop)

4- if property field successful increased, then return

场景:尝试将五个项目上载到blob存储

期望值:表存储中的count属性存储5

问题:成功插入前四项后,代码进入无限循环以插入第五项,并且count属性永远增加。为什么会这样,我不知道。你的任何想法都是好的

#more code
        header_etag = "random-etag"
        response_etag = "random-response"
        while(response_etag != header_etag):
            sleep(random.random())  # sleep between 0 and 1 second.
            header = table_service.get_entity_table(
                client_table, client_table, client_product)
            new_count = header['Count'] + 1
            entity_product = create_product_entity(
                client_table, client_product, new_count, client_image_table)
            header_etag = header['etag']
            try:    
                response_etag = table_service1.merge_entity(client_table, entity_product,
                                                            if_match=header_etag)
            except:
                logging.info("race condition detected")

Tags: theclientifresponsecounttable事件sleep
1条回答
网友
1楼 · 发布于 2024-03-28 08:41:08

尝试使用以下代码在while循环中实现您的参数和逻辑:

val = 0 # Initial value as zero
success = False
    while True:
        val  = val + 1 #incrementing
        CheckValidations = input(‘Check’) #add validations
        if CheckValidations == "abc123":
            success = True # this will allow the loop
            break
        if val > 5:
            break
        print("Please Try Again")
if success == True:
print("Welcome!")

还要查看Azure Python SDK文档中的以下.py文件,其中有一个用于更新、合并和插入表存储中实体的清晰函数

azure-sdk-for-python/sample_update_upsert_merge_entities.py at main · Azure/azure-sdk-for-python (github.com)

请参阅Microsoft documentation以检查如何为创建实体传递准确的参数

有关表格示例的更多信息,请查看Azure示例

storage-table-python-getting-started/table_basic_samples.py at master · Azure-Samples/storage-table-python-getting-started (github.com)

相关问题 更多 >