Properly shutting down websocket using Python SDK

Hello, I’m new to using websockets and I’m having a hard time figuring out how to build my code around the alpaca data streams.

I pulled the example code for python and I have it working. Like “stock_stream.run()” runs just fine. And I can keyboard interrupt it to stop it and I’ve added “stock_stream.stop()” afterwards so that I can rerun and not have the number of symbols per account stack across different websockets.

The problem now is that I’m interested in stopping the websocket/continuing the python script past the “.run” using the code itself instead of using a keyboard command in terminal. I’ve tried putting “stock_stream.stop()”/“stock_stream.close()” in the callback function but it either doesn’t change anything or it gets locked up.

If anyone has working python code that uses the data streams in a more advanced way than the example, that would be extremely helpful. Thank you all!

1 Like

Did you figure it out? I have the same problem upon calling .stop() the websocket is not properly closed