isaackogan / TikTokLive

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

ERROR:root:Connection was lost to the Webcast Websocket Server. Restart the client connection to continue. Disconnected #85

Closed simeonhalsey closed 1 year ago

simeonhalsey commented 1 year ago

Im getting the above error when there is a "time.sleep(60)" command added if a certain comment is triggered.

if event.comment in ('Hello', 'hello'):
    exec(open('OBS_Run.py').read())
    time.sleep(60)
    exec(open('OBS_Main.py').read())

I have a need to make the whole script wait for 1 minute while it completes an action and processees anything else. I'm asuming that is too long and the client is disconnecting.

I've tried adding "timeout_ms (default: 60000) But i still eventually get the error.

I've added

@client.on("disconnect") async def on_disconnect(event: DisconnectEvent): print("Disconnected") exit()

This was to try and end the script then a service handler restarts it but sometimes the script does not quit and just starts consuming all the system memory.

Is there an easier way to do what I'm trying to achieve?

Thanks for your help and also for creating such an awesome project.

isaackogan commented 1 year ago

The issue you are facing is that time.sleep is not thread-safe. It blocks the main thread. TikTokLive uses websocket connections to connect to TikTok, so if you block the main thread, you time out that connection.

Since this entire library is asynchronous, the more appropriate thing to do would be to use await asyncio.sleep(60) which would alleviate your issue and not cause a disconnect.

I hope that helps, please let me know if you have any other questions.