livekit/python-sdks

Event loop is closed when connecting to multiple rooms within the same thread

Closed this issue · 0 comments

With the following snippet, the second attempt would raise a RuntimeError: Event loop is closed

import os
import asyncio
from livekit import rtc

async def main():
    # Token and room details (adjust as needed)
    url = 'ws://localhost:7880/'
    token = os.environ['LIVEKIT_TOKEN']
    room = rtc.Room()

    track_sub = asyncio.Event()

    @room.on("track_subscribed")
    def on_track_subscribed(track: rtc.Track, publication: rtc.RemoteTrackPublication, participant: rtc.RemoteParticipant):
        nonself_track = track not in room.local_participant.tracks
        if track.kind == rtc.TrackKind.KIND_AUDIO and nonself_track:
            stream = rtc.AudioStream(track) # the error comes from this line
            track_sub.set()

    await room.connect(url, token)
    await track_sub.wait()
    await room.disconnect()
    

if __name__ == '__main__':
    asyncio.run(main())
    asyncio.run(main())