我想在Azure Batch中执行一个简单的任务,等待它完成并获取结果:
using (var client = _CreateBatchClient())
{
var monitor = client.Utilities.CreateTaskStateMonitor();
var task = new CloudTask(Guid.NewGuid().ToString(), "echo hello world");
await client.JobOperations.AddTaskAsync("Test", task);
await monitor.WhenAll(new List<CloudTask> { task }, TaskState.Completed, _timeout);
var result = task.ExecutionInformation.Result;
}
和WhenAsync
线抛出System.InvalidOperationException: 'This operation is forbidden on unbound objects.'
消息很模糊,而我离本教程不远。怎么了?
从此代码中看不出来,但实际上Azure Batch在这里不知道如何识别任务。该作业包含任务,但是任务没有对其运行的作业的引用。任务ID也不是全局性地标识任务,它只需要在工作中是唯一的即可。
这可能是“未绑定对象”的含义。监视器只是不明白要看什么。实际上,如果该WhenAsync
行被注释,则下一行将引发类似的InvalidOperationException: 'The property ExecutionInformation cannot be read while the object is in the Unbound state.'
因此正确的方法是通过作业引用任务:
using (var client = _CreateBatchClient())
{
var monitor = client.Utilities.CreateTaskStateMonitor();
var id = Guid.NewGuid().ToString();
var taskToAdd = new CloudTask(id, "echo hello world");
await client.JobOperations.AddTaskAsync("Test", taskToAdd);
var taskToTrack = await client.JobOperations.GetTaskAsync("Test", id);
await monitor.WhenAll(new List<CloudTask> { taskToTrack }, TaskState.Completed, _timeout);
}
比较:
为了获得结果信息,需要再次“找到”作业中的任务,否则它将为null。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句