# Properly shutting down websocket using Python SDK

**URL:** <https://forum.alpaca.markets/t/properly-shutting-down-websocket-using-python-sdk/11950>\
**Category:** Alpaca Market Data\
**Created:** [March 3, 2023, 8:11pm UTC](https://forum.alpaca.markets/t/properly-shutting-down-websocket-using-python-sdk/11950 "2023-03-03T20:11:52Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tony\_Nippolei](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/tony_nippolei/32/5180_2.png) [@Tony\_Nippolei](https://forum.alpaca.markets/u/Tony_Nippolei)\
**Post date:** [March 3, 2023, 8:11pm UTC](https://forum.alpaca.markets/t/properly-shutting-down-websocket-using-python-sdk/11950/1 "2023-03-03T20:11:52Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![David\_P\_Brazda](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/david_p_brazda/32/5021_2.png) [@David\_P\_Brazda](https://forum.alpaca.markets/u/David_P_Brazda)\
**Post date:** [February 23, 2024, 2:03am UTC](https://forum.alpaca.markets/t/properly-shutting-down-websocket-using-python-sdk/11950/2 "2024-02-23T02:03:50Z")

</div>

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