isaackogan / TikTokLive

Python library to receive live stream events (comments, gifts, etc.) in realtime from TikTok LIVE.
https://isaackogan.github.io/TikTokLive/
MIT License
908 stars 177 forks source link

ws client connect error : "RuntimeError: async generator ignored GeneratorExit" #182

Closed juzhesys closed 8 months ago

juzhesys commented 8 months ago

v6.0.1 MacOS Intel py3.8

after client first connect the live room, RuntimeError always raised in 1s-10s, sometimes in 1min (RuntimeError: async generator ignored GeneratorExit) , then the client connection is out.

When reconnecting manually does not solve the problem either

Exception ignored in: <async_generator object Connect.aiter at 0x1350e9820> Traceback (most recent call last): File "/Users/user/.pyenv/versions/3.8.13/lib/python3.8/site-packages/TikTokLive/client/ws/ws_client.py", line 185, in connect_loop yield webcast_message RuntimeError: async generator ignored GeneratorExit

isaackogan commented 8 months ago

I cannot guess without your code.

juzhesys commented 8 months ago

sry... the code as follow...

`class TikTokLiveMonitor(QObject): def init(self, parent=None): super().init(parent) self.tasks = {} self.clients = {}

async def start_monitoring(self, row, url):
    targetid = url
    if "@" in targetid:
        targetid = targetid[targetid.index('@'):].replace('/live','')
    print(f'target:{targetid}')
    client: TikTokLiveClient = TikTokLiveClient(unique_id=targetid,
                                                )

    @client.on(ConnectEvent)
    async def on_connect(_: ConnectEvent):
        unique_id = client.room_id
        print("connected the live room:", unique_id)

    async def on_comment(event: CommentEvent):
        msgtype = 'comment'
        content = event.comment
        user_uniqueid = event.user.unique_id
        print(f'{user_uniqueid} send the comment : {content}')

    client.add_listener(CommentEvent, on_comment)
    client.logger.setLevel(LogLevel.INFO.value)
    await client.start()

    client.logger.info(f"start done {url}")

def add_task(self, row, url):
    loop = asyncio.get_event_loop()
    task = loop.create_task(self.start_monitoring(row, url))
    self.tasks[url] = task

async def stop_monitoring(self, url):
    task = self.tasks.get(url)
    client = self.clients.get(url)
    print(f'[INFO] stop monitoring: {url}')
    if task:
        await client.disconnect()
        task.cancel()
        del self.tasks[url]
        del self.clients[url]`
isaackogan commented 8 months ago

Closed as duplicate of https://github.com/isaackogan/TikTokLive/issues/183