簡體   English   中英

在Azure中編寫(+ lock)/讀取文件的Python腳本

[英]Python script to write(+lock) / read a file in Azure

我是python編程和Azure的新手。

我需要編寫一個將由2個進程執行的腳本。

這兩個進程將運行相同的python腳本。 我知道Azure可以在其中存儲一些文件的storageAccounts,我發現了這一點: https : //docs.microsoft.com/zh-cn/python/api/azure-storage-file/azure.storage.file.fileservice .fileservice?視圖=天青-蟒

和: https : //github.com/Azure/azure-storage-python

這是一些偽代碼來說明我需要實現的目標:

function useStorageFile
   if(fileFromStorage == null)
      createFileInStorage lockFileInStorage;
      executeDockerCommand;
      writeResultOFCommandInStorageFile;
   else
      if(fileFromStorage != null)
        X:if(fileFromStorage.status !== 'locked')
           readResultFromFile
        else
           wait 1s;
           continue X;

是否可以在Azure中鎖定/解鎖文件? 例如,如何在python中實現呢? 謝謝。

編輯我已經設法用python腳本在Blob存儲中寫入文件。 現在的問題是:如何在第一個進程寫入命令結果的同時鎖定文件,並在由Blob存儲鎖(如果存在該選項...)釋放后立即讓第二個進程讀取文件第一個過程? 這是iam使用的python腳本:

import os, uuid, sys
from azure.storage.blob import BlockBlobService, PublicAccess

def run_sample():
    try:
        # Create the BlockBlockService that is used to call the Blob service for the storage account
        block_blob_service = BlockBlobService(account_name='xxxxxx', account_key='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')

        # Create a container called 'quickstartblobs'.
        container_name ='quickstartblobs'
        block_blob_service.create_container(container_name)

        # Set the permission so the blobs are public.
        block_blob_service.set_container_acl(container_name, public_access=PublicAccess.Container)

        # Create a file in Documents to test the upload and download.
        local_path=os.path.abspath(os.path.curdir)
        local_file_name ='youss.txt'
        full_path_to_file =os.path.join(local_path, local_file_name)

        # Write text to the file.
        file = open(full_path_to_file,  'w')
        file.write("Hello, World!")
        file.close()

        print("Temp file = " + full_path_to_file)
        print("\nUploading to Blob storage as blob" + local_file_name)

        # Upload the created file, use local_file_name for the blob name
        block_blob_service.create_blob_from_path(container_name, local_file_name, full_path_to_file)

        # List the blobs in the container
        print("\nList blobs in the container")
        generator = block_blob_service.list_blobs(container_name)
        for blob in generator:
            print("\t Blob name: " + blob.name)

        # Download the blob(s).
        # Add '_DOWNLOADED' as prefix to '.txt' so you can see both files in Documents.
        full_path_to_file2 = os.path.join(local_path, str.replace(local_file_name ,'.txt', '_DOWNLOADED.txt'))
        print("\nDownloading blob to " + full_path_to_file2)
        block_blob_service.get_blob_to_path(container_name, local_file_name, full_path_to_file2)

        sys.stdout.write("Sample finished running. When you hit <any key>, the sample will be deleted and the sample "
                         "application will exit.")
        sys.stdout.flush()
        input()

        # Clean up resources. This includes the container and the temp files
        block_blob_service.delete_container(container_name)
        os.remove(full_path_to_file)
        os.remove(full_path_to_file2)
    except Exception as e:
        print(e)


# Main method.
if __name__ == '__main__':
    run_sample()

我如何在第一個進程寫入命令結果的同時鎖定文件,並在第一個進程釋放Blob存儲鎖(如果存在該選項...)后立即讓第二個進程讀取文件?

Azure Blob存儲具有一項可以稱為“ Lease的功能。 從本質上講, Leasing流程會獲得對資源的獨占鎖定(在您的情況下為blob),並且只有一個流程可以在blob上獲得租約。 在Blob上獲得租約后,任何其他進程都無法修改或刪除Blob。

因此,您需要做的是嘗試在寫入之前在Blob上獲取租約。 如果Blob已被租借,則會返回一個錯誤(HTTP狀態碼412,PreConditionFailed錯誤)。 假設您沒有收到錯誤,則可以繼續更新文件。 文件更新后,您可以手動釋放鎖定(中斷租約或釋放租約),也可以讓租約自動過期。 假設您遇到錯誤,則應等待並定期(例如每5秒一次)獲取Blob的租約狀態。 一旦發現不再租用該Blob,就可以閱讀該Blob的內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM