Ari*_*Ari 3 python azure-functions azure-durable-functions
我的协调器接收一个有效负载,该有效负载包含需要与其他数据集一起传递给活动函数的指令。
如何将多个参数传递给活动函数?还是我必须将所有数据混合在一起?
def orchestrator_function(context: df.DurableOrchestrationContext):
# User defined configuration
instructions: str = context.get_input()
task_batch = yield context.call_activity("get_tasks", None)
# Need to pass in instructions too
parallel_tasks = [context.call_activity("perform_task", task) for task in task_batch]
results = yield context.task_all(parallel_tasks)
return results
Run Code Online (Sandbox Code Playgroud)
该perform_task活动需要来自task_batch和用户输入的项目instructions
我在我的里面做些什么function.json吗?
解决方法 不理想,但我可以将多个参数作为单个元组传递
something = yield context.call_activity("activity", ("param_1", "param_2"))
Run Code Online (Sandbox Code Playgroud)
然后我只需要引用活动中参数的正确索引。
只是为了添加 @Ari 的精彩答案,这里的代码将数据从客户端函数(在本例中为 HTTP 请求)一直传递到活动函数:
Client -> Orchestrator -> Activity
客户
async def main(req: func.HttpRequest, starter: str) -> func.HttpResponse:
client = df.DurableOrchestrationClient(starter)
req_data = req.get_json()
img_url = req_data['img_url']
payload = {"img_url": img_url}
instance_id = await client.start_new(req.route_params["functionName"], None, payload)
logging.info(f"Started orchestration with ID = '{instance_id}'.")
return client.create_check_status_response(req, instance_id)
Run Code Online (Sandbox Code Playgroud)
协调者
def orchestrator_function(context: df.DurableOrchestrationContext):
input_context = context.get_input()
img_url = input_context.get('img_url')
some_response= yield context.call_activity('MyActivity', img_url)
return [some_response]
Run Code Online (Sandbox Code Playgroud)
活动
def main(imgUrl: str) -> str:
print(f'.... Image URL = {imgUrl}')
return imgUrl
Run Code Online (Sandbox Code Playgroud)
似乎没有教科书的方式来做到这一点。我选择给我的单个参数一个通用名称,如parameter或payload。
然后在协调器中传递值时,我这样做:
payload = {"value_1": some_var, "value_2": another_var}
something = yield context.call_activity("activity", payload)
Run Code Online (Sandbox Code Playgroud)
然后在活动功能中,我再次打开它。
编辑:一些埋藏的文档似乎表明https://docs.microsoft.com/en-us/azure/azure-functions/durable/durable-functions-error-handling?tabs=python