Skip to content

Commit 0ece2ca

Browse files
authored
Merge pull request #192722 from NickKouds/nikoudsi/AzureBatchPythonDocUpdate
Modified doc inline code to reflect Python 3 changes from Batch Pytho…
2 parents 16f2193 + 365b75a commit 0ece2ca

File tree

1 file changed

+30
-25
lines changed

1 file changed

+30
-25
lines changed

articles/batch/quick-run-python.md

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ After completing this quickstart, you'll understand key concepts of the Batch se
2121

2222
- A Batch account and a linked Azure Storage account. To create these accounts, see the Batch quickstarts using the [Azure portal](quick-create-portal.md) or [Azure CLI](quick-create-cli.md).
2323

24-
- [Python](https://python.org/downloads) version 2.7 or 3.6 or later, including the [pip](https://pip.pypa.io/en/stable/installing/) package manager.
24+
- [Python](https://python.org/downloads) version 3.6 or later, including the [pip](https://pip.pypa.io/en/stable/installing/) package manager.
2525

2626
## Sign in to Azure
2727

@@ -48,11 +48,11 @@ pip install -r requirements.txt
4848
Open the file `config.py`. Update the Batch and storage account credential strings with the values you obtained for your accounts. For example:
4949

5050
```Python
51-
_BATCH_ACCOUNT_NAME = 'mybatchaccount'
52-
_BATCH_ACCOUNT_KEY = 'xxxxxxxxxxxxxxxxE+yXrRvJAqT9BlXwwo1CwF+SwAYOxxxxxxxxxxxxxxxx43pXi/gdiATkvbpLRl3x14pcEQ=='
53-
_BATCH_ACCOUNT_URL = 'https://mybatchaccount.mybatchregion.batch.azure.com'
54-
_STORAGE_ACCOUNT_NAME = 'mystorageaccount'
55-
_STORAGE_ACCOUNT_KEY = 'xxxxxxxxxxxxxxxxy4/xxxxxxxxxxxxxxxxfwpbIC5aAWA8wDu+AFXZB827Mt9lybZB1nUcQbQiUrkPtilK5BQ=='
51+
BATCH_ACCOUNT_NAME = 'mybatchaccount'
52+
BATCH_ACCOUNT_KEY = 'xxxxxxxxxxxxxxxxE+yXrRvJAqT9BlXwwo1CwF+SwAYOxxxxxxxxxxxxxxxx43pXi/gdiATkvbpLRl3x14pcEQ=='
53+
BATCH_ACCOUNT_URL = 'https://mybatchaccount.mybatchregion.batch.azure.com'
54+
STORAGE_ACCOUNT_NAME = 'mystorageaccount'
55+
STORAGE_ACCOUNT_KEY = 'xxxxxxxxxxxxxxxxy4/xxxxxxxxxxxxxxxxfwpbIC5aAWA8wDu+AFXZB827Mt9lybZB1nUcQbQiUrkPtilK5BQ=='
5656
```
5757

5858
## Run the app
@@ -109,11 +109,8 @@ To interact with a storage account, the app creates a [BlobServiceClient](/pytho
109109

110110
```python
111111
blob_service_client = BlobServiceClient(
112-
account_url="https://{}.{}/".format(
113-
config._STORAGE_ACCOUNT_NAME,
114-
config._STORAGE_ACCOUNT_DOMAIN
115-
),
116-
credential=config._STORAGE_ACCOUNT_KEY
112+
account_url=f"https://{config.STORAGE_ACCOUNT_NAME}.{config.STORAGE_ACCOUNT_DOMAIN}/",
113+
credential=config.STORAGE_ACCOUNT_KEY
117114
)
118115
```
119116

@@ -132,19 +129,19 @@ input_files = [
132129
The app creates a [BatchServiceClient](/python/api/azure.batch.batchserviceclient) object to create and manage pools, jobs, and tasks in the Batch service. The Batch client in the sample uses shared key authentication. Batch also supports Azure Active Directory authentication.
133130

134131
```python
135-
credentials = SharedKeyCredentials(config._BATCH_ACCOUNT_NAME,
136-
config._BATCH_ACCOUNT_KEY)
132+
credentials = SharedKeyCredentials(config.BATCH_ACCOUNT_NAME,
133+
config.BATCH_ACCOUNT_KEY)
137134

138135
batch_client = BatchServiceClient(
139136
credentials,
140-
batch_url=config._BATCH_ACCOUNT_URL)
137+
batch_url=config.BATCH_ACCOUNT_URL)
141138
```
142139

143140
### Create a pool of compute nodes
144141

145142
To create a Batch pool, the app uses the [PoolAddParameter](/python/api/azure-batch/azure.batch.models.pooladdparameter) class to set the number of nodes, VM size, and a pool configuration. Here, a [VirtualMachineConfiguration](/python/api/azure-batch/azure.batch.models.virtualmachineconfiguration) object specifies an [ImageReference](/python/api/azure-batch/azure.batch.models.imagereference) to an Ubuntu Server 20.04 LTS image published in the Azure Marketplace. Batch supports a wide range of Linux and Windows Server images in the Azure Marketplace, as well as custom VM images.
146143

147-
The number of nodes (`_POOL_NODE_COUNT`) and VM size (`_POOL_VM_SIZE`) are defined constants. The sample by default creates a pool of 2 size *Standard_DS1_v2* nodes. The size suggested offers a good balance of performance versus cost for this quick example.
144+
The number of nodes (`POOL_NODE_COUNT`) and VM size (`POOL_VM_SIZE`) are defined constants. The sample by default creates a pool of 2 size *Standard_DS1_v2* nodes. The size suggested offers a good balance of performance versus cost for this quick example.
148145

149146
The [pool.add](/python/api/azure-batch/azure.batch.operations.pooloperations) method submits the pool to the Batch service.
150147

@@ -159,8 +156,8 @@ new_pool = batchmodels.PoolAddParameter(
159156
version="latest"
160157
),
161158
node_agent_sku_id="batch.node.ubuntu 20.04"),
162-
vm_size=config._POOL_VM_SIZE,
163-
target_dedicated_nodes=config._POOL_NODE_COUNT
159+
vm_size=config.POOL_VM_SIZE,
160+
target_dedicated_nodes=config.POOL_NODE_COUNT
164161
)
165162
batch_service_client.pool.add(new_pool)
166163
```
@@ -184,16 +181,17 @@ The app creates a list of task objects using the [TaskAddParameter](/python/api/
184181
Then, the app adds tasks to the job with the [task.add_collection](/python/api/azure-batch/azure.batch.operations.taskoperations) method, which queues them to run on the compute nodes.
185182

186183
```python
187-
tasks = list()
184+
tasks = []
188185

189-
for idx, input_file in enumerate(input_files):
190-
command = "/bin/bash -c \"cat {}\"".format(input_file.file_path)
186+
for idx, input_file in enumerate(resource_input_files):
187+
command = f"/bin/bash -c \"cat {input_file.file_path}\""
191188
tasks.append(batchmodels.TaskAddParameter(
192-
id='Task{}'.format(idx),
189+
id=f'Task{idx}',
193190
command_line=command,
194191
resource_files=[input_file]
195192
)
196193
)
194+
197195
batch_service_client.task.add_collection(job_id, tasks)
198196
```
199197

@@ -207,15 +205,22 @@ tasks = batch_service_client.task.list(job_id)
207205
for task in tasks:
208206

209207
node_id = batch_service_client.task.get(job_id, task.id).node_info.node_id
210-
print("Task: {}".format(task.id))
211-
print("Node: {}".format(node_id))
208+
print(f"Task: {task.id}")
209+
print(f"Node: {node_id}")
212210

213211
stream = batch_service_client.file.get_from_task(
214-
job_id, task.id, config._STANDARD_OUT_FILE_NAME)
212+
job_id, task.id, config.STANDARD_OUT_FILE_NAME)
215213

216214
file_text = _read_stream_as_string(
217215
stream,
218-
encoding)
216+
text_encoding)
217+
218+
if text_encoding is None:
219+
text_encoding = DEFAULT_ENCODING
220+
221+
sys.stdout = io.TextIOWrapper(sys.stdout.detach(), encoding = text_encoding)
222+
sys.stderr = io.TextIOWrapper(sys.stderr.detach(), encoding = text_encoding)
223+
219224
print("Standard output:")
220225
print(file_text)
221226
```

0 commit comments

Comments
 (0)