How to stream ouput form `compute_client.virtual_machines.begin_run_command` in real time

Tinnapop 40 Reputation points
2024-02-23T02:40:03.8833333+00:00

I want to stream the output from poller on azure vm, assume that my Python file is: for i in range(10000): print(i) I want the output: 0 1 2 3 4 Just like I run this command in VM directly.

import os
import logging
from dotenv import load_dotenv
from azure.identity import DefaultAzureCredential
from azure.mgmt.compute import ComputeManagementClient

# --------------------------------------------------------------------------- #
#                               Define functions                              #
# --------------------------------------------------------------------------- #
load_dotenv()
# Acquire a credential object.
credential = DefaultAzureCredential()

# Retrieve subscription ID from environment variable.
subscription_id = os.getenv("AZURE_SUBSCRIPTION_ID")

# VM
vm_name = os.getenv("AZURE_VM_NAME")
resource_group = os.getenv("AZURE_RESOURCE_GROUP")

compute_client = ComputeManagementClient(credential, subscription_id)
run_command_parameters = {
    'command_id': 'RunShellScript', # For linux, don't change it
    'script': [
        'python3 /home/xxx/test.py'
    ]
}

poller = compute_client.virtual_machines.begin_run_command(
        resource_group,
        vm_name,
        run_command_parameters
)


Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
9,021 questions
{count} votes

1 answer

Sort by: Most helpful
  1. deherman-MSFT 38,021 Reputation points Microsoft Employee Moderator
    2024-02-26T16:55:31.17+00:00

    @Tinnapop
    Apologies for the delayed response. Based on my research streaming the output from compute_client.virtual_machines.begin_run_command in real time is not supported by Azure. The begin_run_command method returns a long-running operation poller object that can be used to check the status or get the result of the command execution. However, the poller object does not provide a way to stream the output as it is being generated by the script. The output is only available after the command execution is completed or failed.
    One possible workaround is to use the output_blob_uri parameter of the run_command_parametersdictionary to specify an Azure storage blob where the script output stream will be uploaded. This way, you can monitor the blob content while the script is running and get the output in real time.

    Hope this helps! Let me know if you have further questions.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.