Microsoft.Azure.WebJobs.Script.Workers.Rpc.RpcException:结果:失败异常:TypeError:协程类型的对象不可 JSON 序列化

问题描述 投票:0回答:1

我正在尝试创建一个 Durable Functions 应用程序,该应用程序使用 Azure Function App 中的 Python 来利用 HTTP 触发器 API。但是,我遇到错误“Microsoft.Azure.WebJobs.Script.Workers.Rpc.RpcException:结果:失败异常:TypeError:协程类型的对象不是 JSON 可序列化”。下面是代码:

import azure.functions as func
import azure.durable_functions as df
from common_config.scripts import shared_config as sc
from xml_remit_gen import app_controller

myApp = df.DFApp(http_auth_level=func.AuthLevel.ANONYMOUS)

# Set configurations function (now synchronous)
def set_configurations(req_body):
    sc.GV_JOBCHAIN = req_body.get('GV_JOBCHAIN')
    sc.GV_XML_SPLIT_REQ = req_body.get('GV_XML_SPLIT_REQ')
    sc.GV_SKIP_THRESHOLD_LIMIT_CHECK_YN = req_body.get('GV_SKIP_THRESHOLD_LIMIT_CHECK_YN')
    sc.BATCH_SIZE = req_body.get('BATCH_SIZE')
    sc.PARAM_MINPROCESS_ID = req_body.get('INSERT_PROCESS_ID')


# HTTP-Triggered Function with a Durable Functions Client binding
@myApp.route(route="orchestrators/xml_remit_gen", methods=["POST"])
@myApp.durable_client_input(client_name="client")
async def http_xml_remit_gen(req: func.HttpRequest, client: df.DurableOrchestrationClient):
    try:
        req_body = req.get_json()
        set_configurations(req_body)  # Call the sync function
        print(req_body)
        # Assuming no additional arguments are required for start_new
        instance_id = await client.start_new("hello_orchestrator", req_body)
        response = client.create_check_status_response(req, instance_id)
    except Exception as e:
        return func.HttpResponse(f"Error starting orchestration: {str(e)}", status_code=500)



# Orchestrator
@myApp.orchestration_trigger(context_name="context")
async def hello_orchestrator(context: df.DurableOrchestrationContext):
    req_body = context.get_input()
    await df.call_activity("run_xml_remit_gen", req_body)  # Pass req_body directly
    return req_body  # Not necessary to return req_body here


# Activity
@myApp.activity_trigger(input_name="reqBody")
async def run_xml_remit_gen(reqBody: str):
    await app_controller.xml_remit_gen_main()  # Assuming this is a valid async function
    return reqBody

我尝试过逐行调试,当我到达下面的行时,我看到它抛出错误

response = client.create_check_status_response(req, instance_id)

python azure azure-functions azure-durable-functions
1个回答
0
投票

TypeError:协程类型的对象不可 JSON 序列化

错误“TypeError:协程类型的对象不是 JSON 可序列化”,表示您正在尝试将协程对象序列化为 JSON。

问题可能是您尝试在

client.create_check_status_response(req, instance_id)
函数中使用
http_xml_remit_gen
创建响应。这个函数是异步的,你正在等待它,这意味着它返回一个协程对象。

  • There is another approach
    解决这个问题,
    By using await the response creation separately before returning it

修改后的代码

@myApp.route(route="orchestrators/xml_remit_gen", methods=["POST"])
@myApp.durable_client_input(client_name="client")
async def http_xml_remit_gen(req: func.HttpRequest, client: df.DurableOrchestrationClient):
    try:
        req_body = req.get_json()
        set_configurations(req_body)  # Call the sync function
        print(req_body)
                instance_id = await client.start_new("hello_orchestrator", req_body)
        # Await the response creation separately
        response = await client.create_check_status_response(req, instance_id)
        return response  # Return the response after awaiting
    except Exception as e:
        return func.HttpResponse(f"Error starting orchestration: {str(e)}", status_code=500)
  • 通过在上述代码的响应中使用
    await
    将解决 JSON 序列化错误。
© www.soinside.com 2019 - 2024. All rights reserved.