ThreadedWebsocketManager()
pdb159 opened this issue · 1 comments
Describe the bug
I'm using python-binance's example of a threaded websocket manager, to retrieve BTCUSDT prices. When running the script within jupyter lab i receive the follwoing RuntimeError:Exception in thread Thread-9:
Traceback (most recent call last):
File "C:\Users\pb\Documents\Python_Anaconda3\envs\CryptoPredictor\lib\threading.py", line 1016, in _bootstrap_inner
self.run()
File "C:\Users\pb\Documents\Python_Anaconda3\envs\CryptoPredictor\lib\site-packages\binance\threaded_stream.py", line 59, in run
self._loop.run_until_complete(self.socket_listener())
File "C:\Users\pb\Documents\Python_Anaconda3\envs\CryptoPredictor\lib\asyncio\base_events.py", line 625, in run_until_complete
self._check_running()
File "C:\Users\pb\Documents\Python_Anaconda3\envs\CryptoPredictor\lib\asyncio\base_events.py", line 584, in _check_running
raise RuntimeError('This event loop is already running')
RuntimeError: This event loop is already running
If running it in PyCharm i don't receive any errors but also no output and nothing seems to be happening.
To Reproduce
`import time
from binance import ThreadedWebsocketManager
api_key = '<api_key'>
api_secret = '<api_secret'>
def main():
symbol = 'BNBBTC'
twm = ThreadedWebsocketManager(api_key=api_key, api_secret=api_secret)
# start is required to initialise its internal loop
twm.start()
def handle_socket_message(msg):
print(f"message type: {msg['e']}")
print(msg)
twm.start_kline_socket(callback=handle_socket_message, symbol=symbol)
# multiple sockets can be started
twm.start_depth_socket(callback=handle_socket_message, symbol=symbol)
# or a multiplex socket can be started like this
# see Binance docs for stream names
streams = ['bnbbtc@miniTicker', 'bnbbtc@bookTicker']
twm.start_multiplex_socket(callback=handle_socket_message, streams=streams)
twm.join()
if name == "main":
main()`
Expected behavior
A clear and concise description of what you expected to happen.
Environment (please complete the following information):
- Python version: [e.g. 3.5]
- Virtual Env: [e.g. virtualenv, conda]
- OS: [e.g. Mac, Ubuntu]
- python-binance version
Logs or Additional context
Jupyter Lab Python Setup:
- Python = 3.10.13
- jupyter lab = 4.0.10
- python-binance = 1.0.19
PyCharm Setup:
- Python = 3.9.13
- python-binance = 1.0.19
Any Ideas on how to fix this? I saw different posts about this from 2022 and 2023 and was wondering if it is still the same fix, replacing some line of code within the python-binance library
On streams.py#L87, adding the ssl parameter as ssl=ssl_context
can resolve the issue.
self._conn = ws.connect(ws_url, close_timeout=0.1, ssl=ssl_context)
To define the ssl_context
, can use the following setup:
import ssl
ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
ssl_context.check_hostname = False
ssl_context.verify_mode = ssl.CERT_NONE
# or
import certifi
ssl_context = ssl.create_default_context(cafile=certifi.where())
And adding the session_params
:
ThreadedWebsocketManager(api_key=api_key, api_secret=api_secret, session_params={'connector':aiohttp.TCPConnector(ssl=ssl_context)})