# Stream data goes into a loop with Trade API client (python, flask)

**URL:** <https://forum.alpaca.markets/t/stream-data-goes-into-a-loop-with-trade-api-client-python-flask/7680>\
**Category:** Alpaca Market Data\
**Created:** [December 11, 2021, 1:12am UTC](https://forum.alpaca.markets/t/stream-data-goes-into-a-loop-with-trade-api-client-python-flask/7680 "2021-12-11T01:12:01Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![s2j1968](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/s2j1968/32/1437_2.png) [@s2j1968](https://forum.alpaca.markets/u/s2j1968)\
**Post date:** [December 11, 2021, 1:12am UTC](https://forum.alpaca.markets/t/stream-data-goes-into-a-loop-with-trade-api-client-python-flask/7680/1 "2021-12-11T01:12:01Z")

</div>

Using alpaca\_trade\_api python client, am subscribing to crypto & stock stream data. In python, flask environment on re/start, my stream subscription via the alpaca\_trade\_api stream client goes into a loop forever…

RuntimeError should be caught in this loop and graceful exit and restart should occur. Looks like a bug.  
-sj  
--------- trace ----  
RuntimeError: cannot schedule new futures after shutdown  
starting crypto data websocket connection  
error during websocket communication: cannot schedule new futures after shutdown  
Traceback (most recent call last):  
File “/Users/sj/opt/anaconda3/lib/python3.8/site-packages/alpaca\_trade\_api/stream.py”, line 215, in \_run\_forever  
await self.\_start\_ws()  
File “/Users/sj/opt/anaconda3/lib/python3.8/site-packages/alpaca\_trade\_api/stream.py”, line 74, in \_start\_ws  
await self.\_connect()  
File “/Users/sj/opt/anaconda3/lib/python3.8/site-packages/alpaca\_trade\_api/stream.py”, line 51, in \_connect  
self.\_ws = await websockets.connect(  
File “/Users/sj/opt/anaconda3/lib/python3.8/site-packages/websockets/client.py”, line 535, in **await\_impl**  
transport, protocol = await self.\_create\_connection()  
File “/Users/sj/opt/anaconda3/lib/python3.8/asyncio/base\_events.py”, line 986, in create\_connection  
infos = await self.\_ensure\_resolved(  
File “/Users/sj/opt/anaconda3/lib/python3.8/asyncio/base\_events.py”, line 1365, in \_ensure\_resolved  
return await loop.getaddrinfo(host, port, family=family, type=type,  
File “/Users/sj/opt/anaconda3/lib/python3.8/asyncio/base\_events.py”, line 825, in getaddrinfo  
return await self.run\_in\_executor(  
File “/Users/sj/opt/anaconda3/lib/python3.8/asyncio/base\_events.py”, line 783, in run\_in\_executor  
executor.submit(func, \*args), loop=self)  
File “/Users/sj/opt/anaconda3/lib/python3.8/concurrent/futures/thread.py”, line 179, in submit  
raise RuntimeError(‘cannot schedule new futures after shutdown’)  
RuntimeError: cannot schedule new futures after shutdown  
starting crypto data websocket connection  
error during websocket communication: cannot schedule new futures after shutdown  
Traceback (most recent call last):  
File “/Users/sj/opt/anaconda3/lib/python3.8/site-packages/alpaca\_trade\_api/stream.py”, line 215, in \_run\_forever  
await self.\_start\_ws()  
File “/Users/sj/opt/anaconda3/lib/python3.8/site-packages/alpaca\_trade\_api/stream.py”, line 74, in \_start\_ws  
await self.\_connect()  
File “/Users/sj/opt/anaconda3/lib/python3.8/site-packages/alpaca\_trade\_api/stream.py”, line 51, in \_connect  
self.\_ws = await websockets.connect(  
File “/Users/sj/opt/anaconda3/lib/python3.8/site-packages/websockets/client.py”, line 535, in **await\_impl**  
transport, protocol = await self.\_create\_connection()  
File “/Users/sj/opt/anaconda3/lib/python3.8/asyncio/base\_events.py”, line 986, in create\_connection  
infos = await self.\_ensure\_resolved(  
File “/Users/sj/opt/anaconda3/lib/python3.8/asyncio/base\_events.py”, line 1365, in \_ensure\_resolved  
return await loop.getaddrinfo(host, port, family=family, type=type,  
File “/Users/sj/opt/anaconda3/lib/python3.8/asyncio/base\_events.py”, line 825, in getaddrinfo  
return await self.run\_in\_executor(  
File “/Users/sj/opt/anaconda3/lib/python3.8/asyncio/base\_events.py”, line 783, in run\_in\_executor  
executor.submit(func, \*args), loop=self)  
File “/Users/sj/opt/anaconda3/lib/python3.8/concurrent/futures/thread.py”, line 179, in submit  
raise RuntimeError(‘cannot schedule new futures after shutdown’)  
RuntimeError: cannot schedule new futures after shutdown

---

<div class="post-metadata">

**Author:** ![Vobliss](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/vobliss/32/3688_2.png) [@Vobliss](https://forum.alpaca.markets/u/Vobliss)\
**Post date:** [March 12, 2022, 6:45am UTC](https://forum.alpaca.markets/t/stream-data-goes-into-a-loop-with-trade-api-client-python-flask/7680/2 "2022-03-12T06:45:46Z")

</div>

Did you figure this out?
