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/
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](https://private-user-images.githubusercontent.com/134359725/289115259-7cef811e-1ef5-47cd-b029-7c5373adffd7.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjAwNzk5ODEsIm5iZiI6MTcyMDA3OTY4MSwicGF0aCI6Ii8xMzQzNTk3MjUvMjg5MTE1MjU5LTdjZWY4MTFlLTFlZjUtNDdjZC1iMDI5LTdjNTM3M2FkZmZkNy5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjQwNzA0JTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI0MDcwNFQwNzU0NDFaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT03ZjQ4ODcxYWUyOWUwZTM0ZjM2OGFmN2Y1MTViZjQyYjdiNTNkZjM0NDQ3YjBkNmQ1ODliNjNhMGI3OTk4YTkxJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCZhY3Rvcl9pZD0wJmtleV9pZD0wJnJlcG9faWQ9MCJ9.U5pSQbxHtHanHNSvdWzJiJKZmss6UiVTyQLHte5hlqA)
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.
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.