how to add async handler of participant_disconnect...
# ask-ai
f
how to add async handler of participant_disconnected event in python agents sdk ? job_ctx.room.on("participant_disconnected", async_handler) I am getting error
Cannot register an async callback with
.on()`. Use
asyncio.create_task
within your synchronous callback instead.`
t
Thanks for reaching out, Dmitry. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To handle the
participant_disconnected
event asynchronously in Python using the LiveKit SDK, you can use
asyncio.create_task
inside a synchronous callback. Here’s a friendly little guide to help you set it up: 1. First, define a synchronous event handler for the
participant_disconnected
event:
Copy code
@room.on("participant_disconnected")
def on_participant_disconnected(participant):
    logging.info(f"Participant disconnected: {participant.sid} {participant.identity}")
    # Schedule an asynchronous task
    asyncio.create_task(async_cleanup(participant))
2. Next, implement the asynchronous cleanup function:
Copy code
async def async_cleanup(participant):
    # Perform your async operations here
    await some_async_operation(participant)
This way, your async tasks run smoothly without blocking the event loop. If you have any questions or want to chat more about this, just let me know—I’m here to help! Sources: livekit-rtc/livekit/rtc/room.py | terminal-client/client.py