# Stream close() complains "This event loop is already running"

**URL:** <https://forum.alpaca.markets/t/stream-close-complains-this-event-loop-is-already-running/4104>\
**Category:** App-Integration\
**Created:** [January 27, 2021, 9:12pm UTC](https://forum.alpaca.markets/t/stream-close-complains-this-event-loop-is-already-running/4104 "2021-01-27T21:12:03Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![FrankZhao](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/frankzhao/32/1922_2.png) [@FrankZhao](https://forum.alpaca.markets/u/FrankZhao)\
**Post date:** [January 27, 2021, 9:12pm UTC](https://forum.alpaca.markets/t/stream-close-complains-this-event-loop-is-already-running/4104/1 "2021-01-27T21:12:03Z")

</div>

Hi, I am new to Alpaca and have a problem while trying to close StreamConn properly.

I created a thread to wrap the StreamConn. I do the following while try to close the conn.

“”"  
def stop(self, renew=False):  
asyncio.get\_event\_loop().run\_until\_complete(  
self.\_\_conn.close(renew=renew)  
)  
“”"

The error message is:

> Blockquote  
> 2021-01-27 13:05:01,654 streamclient [ERROR] Error stopping websocket client: This event loop is already running.  
> Exception in thread Thread-1:  
> Traceback (most recent call last):  
> File “/usr/lib/python3.8/threading.py”, line 932, in \_bootstrap\_inner  
> self.run()  
> File /pyalgotrade/user\_strategies/alpaca\_trade/stream\_client.py", line 151, in run  
> self.\_\_client.run()  
> File “/pyalgotrade/user\_strategies/alpaca\_trade/stream\_client.py”, line 111, in run  
> self.\_\_conn.run(  
> File “/pyalgotrade/env/lib/python3.8/site-packages/alpaca\_trade\_api/stream2.py”, line 335, in run  
> loop.run\_until\_complete(self.consume())  
> File “/usr/lib/python3.8/asyncio/base\_events.py”, line 608, in run\_until\_complete  
> return future.result()  
> asyncio.exceptions.CancelledError

Thank you for the help

---

<div class="post-metadata">

**Author:** ![FrankZhao](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/frankzhao/32/1922_2.png) [@FrankZhao](https://forum.alpaca.markets/u/FrankZhao)\
**Post date:** [February 4, 2021, 5:50am UTC](https://forum.alpaca.markets/t/stream-close-complains-this-event-loop-is-already-running/4104/2 "2021-02-04T05:50:19Z")

</div>

I tried a much simpler case but still have the issue. Here is the full code.

conn = StreamConn()  
print(“Create a thread for the conn”)  
conn\_thread = threading.Thread(target=conn.run)  
print(“Before thread start”)  
conn\_thread.start()  
print(“After thread start”)

print(“Sleep for 1second”)  
time.sleep(1)

loop = conn.loop  
loop.run\_until\_complete(conn.close(renew=False))

The print out is:  
“”  
Create a thread for the client  
Before thread start  
After thread start  
Sleep for 1second  
Traceback (most recent call last):  
File “/usr/lib/python3.8/runpy.py”, line 192, in \_run\_module\_as\_main  
2021-02-03 21:42:45,786 root [ERROR] error while consuming ws messages: ‘NoneType’ object has no attribute ‘consume’  
return \_run\_code(code, main\_globals, None,  
File “/usr/lib/python3.8/runpy.py”, line 85, in \_run\_code  
exec(code, run\_globals)  
File “/home/USER/pyalgotrade/user\_strategies/test/test\_stream\_client.py”, line 168, in   
loop.run\_until\_complete(client.close(renew=False))  
File “/usr/lib/python3.8/asyncio/base\_events.py”, line 595, in run\_until\_complete  
self.run\_forever()  
File “/usr/lib/python3.8/asyncio/base\_events.py”, line 550, in run\_forever  
raise RuntimeError(‘This event loop is already running’)  
RuntimeError: This event loop is already running  
“”
