0

Python プログラミングと Azure は初めてです。

2 つのプロセスで実行されるスクリプトを作成する必要があります。

2 つのプロセスは同じ python スクリプトを実行します。Azureにはいくつかのファイルを入れるためのstorageAccountsがあることを知っています。これを見つけました: https://docs.microsoft.com/en-us/python/api/azure-storage-file/azure.storage.file.fileservice .fileservice?view=azure-python

および: 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でそれを達成するにはどうすればよいですか? ありがとうございました。

EDIT Pythonスクリプトを使用してBlob Storageにファイルを書き込むことができました。問題は次のとおりです。コマンドの結果を最初のプロセスで書き込んでいる間にファイルをロックし、Blob Storage ロック (オプションが存在する場合...) が解放されるとすぐに 2 番目のプロセスで読み取らせるにはどうすればよいですか?最初のプロセス?これが使用している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()
4

1 に答える 1

1

最初のプロセスでコマンド結果を書き込んでいる間にファイルをロックし、Blob Storage ロック (オプションが存在する場合...) が最初のプロセスによって解放されるとすぐに、2 番目のプロセスで読み取られるようにするにはどうすればよいですか?

Azure Blob Storage には、Lease利用できるという機能があります。基本的Leasingに、プロセスはリソース (この場合は BLOB) の排他ロックを取得し、1 つのプロセスのみが BLOB のリースを取得できます。BLOB でリースが取得されると、他のプロセスは BLOB を変更または削除できなくなります。

したがって、書き込む前に BLOB のリースを取得する必要があります。BLOB が既にリースされている場合は、エラーが返されます (HTTP ステータス コード 412、PreConditionFailed エラー)。エラーが発生しなければ、ファイルの更新を続行できます。ファイルが更新されたら、手動でロックを解放する (リースを中断するか、リースを解放する) か、リースを自動期限切れにすることができます。エラーが発生した場合は、BLOB のリース ステータスを定期的に (たとえば 5 秒ごとに) 取得する必要があります。BLOB がもうリースされていないことがわかったら、BLOB の内容を読み取ることができます。

于 2019-05-21T04:41:45.510 に答える