Microsoft.Azure.WebJobs.Script.Workers.Rpc.RpcException: 结果: 失败 异常: TypeError: 协程对象无法被JSON序列化

0 投票
1 回答
76 浏览
提问于 2025-04-13 02:48

我正在尝试创建一个持久化函数应用,这个应用使用Python在Azure Function App中通过HTTP触发API。不过,我遇到了一个错误,错误信息是'Microsoft.Azure.WebJobs.Script.Workers.Rpc.RpcException: Result: Failure Exception: TypeError: Object of type coroutine is not JSON serializable'。下面是我的代码:

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)

1 个回答

1

类型错误:协程对象无法被序列化为JSON

这个错误“类型错误:协程对象无法被序列化为JSON”意思是你在试图把一个协程对象转成JSON格式。

问题可能出在你在http_xml_remit_gen这个函数里,使用了client.create_check_status_response(req, instance_id)来创建响应。这个函数是异步的,你在等待它完成,这样它就返回了一个协程对象。

  • 有另一种方法可以解决这个问题,就是在返回之前单独使用await来创建响应

修改后的代码

@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序列化错误。

撰写回答