API Designer and async function

F61395A9B8
F61395A9B8 Registered Posts: 1

Hello, i'm trying to run a background task when my endpoint is call.

Here is a simple example of what i am trying to do.

import asyncio

async def run_task_background(parameters):
job_uid = await run_task(parameters)
#add job_uid in a table def api_py_function(): asyncio.run(run_task_background(parameters))
return {"status": "Creating task"}

I tried a lot of different formulation but i always receive an error :

Failed to run function : <class 'TypeError'> : Object of type coroutine is not JSON serializable

Does somebody know what i am doing wrong ?

Thank you

Answers

  • Lautaro
    Lautaro Registered Posts: 2

    Hi,

    I tried to reproduce this issue by simulating an async task and calling it, and it worked fine:

    import asyncio
    
    async def run_task(parameters):
        await asyncio.sleep(5)  # Simulating async work
        return f"Task completed with parameters: {parameters}"
    
    async def run_task_background(parameters):
    
        job_uid = await run_task(parameters)
    
    def api_py_function(parameters):
      asyncio.run(run_task_background(parameters))
    
      return {"status": "Creating task"}
    

    Result: 

    {"status":"Creating task"}

    Could you try this by adding the parameter in your api_py_function and see if the same error occurs?

Setup Info
    Tags
      Help me…