Python Websoket Exit after market close and reconnection

pawan12
hi all i have two questions related to wesoket python client.

First:- below snippit is from Example code.
# Infinite loop on the main thread. Nothing after this will run.
# You have to use the pre-defined callbacks to manage subscriptions.
kws.connect()

###my question here is if its infinite loop and nothing after this will run than how any code will close seamlessly after its work is done. i tried using datetime.timenow and call ws.close() but it was giving me error like "Connection error: 1006 - connection was closed uncleanly " what is the correct way to close connection after my system closes at 3.30.

2nd question :-1:
once a connection is established how do i know if it gets disconnected inbetween and how to reconnect it back i read that by default auto re-connection is enabled but how do we know its disconnected and how do we know its reconnected back ?

  • rakeshr
    @pawan12
    my question here is if its infinite loop and nothing after this will run than how any code will close seamlessly after its work is done
    As it's continuos call back, so if you want to do any external calculation. Either you can use Queue in the same thread or create an external thread and do the required computation. Go through this Websocket Streaming Python FAQs.
    what is the correct way to close connection after my system closes at 3.30.
    You can call close method with time condition, go through this documentation . Either in main thread or external thread, as stated above.
    how do we know its disconnected and how do we know its reconnected back ?
    You can log each connection reconnection at your end and accordingly filter the same. Example Websocket code here, uses logging.
  • pawan12
    @rakeshr thanks for your reply but as i said in my first post if i call ws.close() method based on time condition it gives this error Connection error: 1006 - connection was closed uncleanly " do we need to do something else before we close the connection using ws.close()
  • rakeshr
    @pawan12
    You can go through this thread.
Sign In or Register to comment.