Environment
Python
Alpaca SDK Version
.48
Other Environment Details
Using alpaca_trade_api. I have my Alpaca key, etc as environment variables. I have no trouble with the REST API in the same app.
Problem
When running a websocket stream in another thread, I get the following error:
RuntimeError: There is no current event loop in thread ‘Thread-1’.
Paper or Live Tradng?
Paper
Example Code
Here’s my code:
app.py
def start_listener():
listener = alpaca.alpacaListener()
listener.run()
x = threading.Thread(target=start_listener, daemon=True)
x.start()
alpaca.py
class alpacaListener (object):
def __init__(self):
self.api = API()
def run(self):
try:
log.info('Initializing worker WebSocket Connection')
conn = self.api.StreamConn
except:
log.error('Unable to Initialize worker Websocket connection',exc_info=True)
@conn.on(r'trade_updates')
async def on_msg(conn, channel, data):
print(data)
conn.run(['trade_updates'])