livekit / python-sdks

LiveKit real-time and server SDKs for Python

Home Page:https://docs.livekit.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Sync version of livekit api

huynguyen-ff opened this issue · comments

Hi, thanks for the good work.
I just wonder can we have a version of sync api of the LiveKit api (together with async api). So that we can call api in some sync framework such as Django or Celery? The 2 version sync and async live in one repo is just like some famous packages such as Redis, Google cloud, OpenAI.

i don't think that this is necessary for Django specifically, because it has support for asynchronous functions.
https://docs.djangoproject.com/en/4.2/topics/async/

Hi, because when I use async_to_sync to wrap the client.room.create_room function, it always raises the exception below.

image

The exception raised from /site-packages/livekit/api/_twirp_client.py:86, in TwirpClient.request(self, service, method, data, headers, response_class)

image

can you show the snippet of the code where you wrap client.room.create_room?

In my case i created a function called create_room with async def and used "await client.room.create_room" inside. Then in my django view i used async_to_sync(create_room)() and it works fine.

Hi @Gabriel-rd2 , in my case, I use create_room function wrapped async_to_sync inside a celery task. For other async_to_sync (like django channels) it works normally but for this function, there's exception raised RuntimeError: Timeout context manager should be used inside a task like I said above.

image

Could you try moving client.room.create_room to another function, and then use async_to_sync on it? like

async def createRoom(name): await client.room.create_room(CreateRoomRequest(name=name) )

And then on your task do:
async_to_sync(createRoom)(f"room-{room_id}")

This timeout error could be something else though

Agreed with @Gabriel-rd2, you could use async_to_sync or other techniques to handle the async lib. We have a small team and will not able to maintain two versions of the library at the moment.

Hi @huynguyen-ff

I used async_to_sync, but still got the error 'RuntimeError: Timeout context manager should be used inside a task'
Have you fixed this error? Please guide for me. Thank you.

@TheNha , because I used django-channel so I created an async worker for me and I move the livekit api into that worker.

@huynguyen-ff Yeppp. Can you share the function, which you have created an async worker.
Thank you very much.

@TheNha I used this one https://channels.readthedocs.io/en/latest/topics/worker.html because I used websocket channel. You can use any others as long as it support asyncio (async function).

@TheNha I used this one https://channels.readthedocs.io/en/latest/topics/worker.html because I used websocket channel. You can use any others as long as it support asyncio (async function).

I'm using flask, is there a better way to do it? I've tried workaround from Stack overflow. But it doesn't work.