Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic ThreadedWebsocketManager example doesn't work. #1243

Closed
Enebz opened this issue Sep 2, 2022 · 12 comments · Fixed by #1449
Closed

Basic ThreadedWebsocketManager example doesn't work. #1243

Enebz opened this issue Sep 2, 2022 · 12 comments · Fixed by #1449

Comments

@Enebz
Copy link
Contributor

Enebz commented Sep 2, 2022

Describe the bug
Basic example of ThreadedWebsocketManager doesn't work.
The console remains empty after long waiting.

To Reproduce
Code snippet to reproduce the behavior:

import time

from binance import ThreadedWebsocketManager

api_key = ''
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)
    twm.start_depth_socket(callback=handle_socket_message, symbol=symbol)

    twm.join()


if __name__ == "__main__":
   main()

Expected behavior
I expected it to work :)

Environment (please complete the following information):

  • Python version: 3.10.6 (64-bit)
  • Virtual Env: Not using any
  • OS: Windows 10
  • python-binance: 1.0.16
@ImWez
Copy link

ImWez commented Sep 2, 2022

I have same issue.

I solved using python 3.9 and python-binance==1.0.15 in Virtual Env

@Enebz
Copy link
Contributor Author

Enebz commented Sep 3, 2022

Yes, I figured that doing this was going to make it work, but I'd like to be able to use the latest versions...

@Heissluftofen
Copy link

Thank you very much. My bot didn't run since last week because i did some updates. Why on earth did they change something which lead to not working websockets or threats anymore.

@Enebz
Copy link
Contributor Author

Enebz commented Sep 26, 2022

@Heissluftofen What type of bot are you making? I am making a bot too. Maybe we can chat on discord or something?

@kolexander
Copy link

I have python-binance==1.0.16
The console remains empty after long waiting too.
When I use python-binance==1.0.15
I got
Traceback (most recent call last): File "/home/kolex/www/cryptobot/pblastprice.py", line 33, in <module> bsm.start_symbol_ticker_socket(callback=btc_trade_history, symbol='BTCUSDT') File "/home/kolex/.pyenv/versions/3.10.7/lib/python3.10/site-packages/binance/streams.py", line 1288, in start_symbol_ticker_socket return self._start_async_socket( File "/home/kolex/.pyenv/versions/3.10.7/lib/python3.10/site-packages/binance/streams.py", line 1195, in _start_async_socket socket = getattr(self._bsm, socket_name)(**params) File "/home/kolex/.pyenv/versions/3.10.7/lib/python3.10/site-packages/binance/streams.py", line 757, in symbol_ticker_socket return self._get_socket(symbol.lower() + '@ticker') File "/home/kolex/.pyenv/versions/3.10.7/lib/python3.10/site-packages/binance/streams.py", line 346, in _get_socket self._conns[conn_id] = ReconnectingWebsocket( File "/home/kolex/.pyenv/versions/3.10.7/lib/python3.10/site-packages/binance/streams.py", line 62, in __init__ self._queue = asyncio.Queue(loop=self._loop) File "/home/kolex/.pyenv/versions/3.10.7/lib/python3.10/asyncio/queues.py", line 34, in __init__ super().__init__(loop=loop) File "/home/kolex/.pyenv/versions/3.10.7/lib/python3.10/asyncio/mixins.py", line 17, in __init__ raise TypeError( TypeError: As of 3.10, the *loop* parameter was removed from Queue() since it is no longer necessary ^CException ignored in: <module 'threading' from '/home/kolex/.pyenv/versions/3.10.7/lib/python3.10/threading.py'> Traceback (most recent call last): File "/home/kolex/.pyenv/versions/3.10.7/lib/python3.10/threading.py", line 1567, in _shutdown lock.acquire()

@Enebz
Copy link
Contributor Author

Enebz commented Oct 5, 2022

@kolexander

You have to use Python 3.9 to use python-binance 1.0.15.

The reason lies in your error message:

TypeError: As of 3.10, the loop parameter was removed from Queue()

@Heissluftofen
Copy link

Heissluftofen commented Oct 11, 2022 via email

@Sladerix
Copy link

I have same issue.

I solved using python 3.9 and python-binance==1.0.15 in Virtual Env

It pointed me in the right way.... but I noticed that with python-binance==1.0.16 (latest) it does not work anymore.
it gives this error.

RuntimeError: Task <Task pending name='Task-27' coro=<Queue.get() running at /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/queues.py:166> cb=[_release_waiter(<Future pendi...1039faf10>()]>)() at /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/tasks.py:416]> got Future <Future pending> attached to a different loop

python 3.9 and python-binance==1.0.15
python 3.9 and python-binance==1.0.16
python 3.10 and python-binance==1.0.16

@viper7882
Copy link

+1 for me using python 3.10 and python-binance==1.0.16 in Windows 11. It seems like this issue has been discovered quite a long while but nobody is looking into it. Does @Enebz expect all users not to move on to the latest versions?

@Enebz
Copy link
Contributor Author

Enebz commented Mar 20, 2023

If you want to look into it, go ahead. I provided a temporary way to work with the library, I am not interested in working to patch this issue.

@chan1919
Copy link

chan1919 commented Jun 9, 2023

The same problem,how to fix the bug with lastest version of Python and python-binance.

@WonniPooh
Copy link

Was able to fixed that issue by:

replaced
self._handle_read_loop = self._loop.call_soon_threadsafe(asyncio.create_task, self._read_loop())
to
self._handle_read_loop = asyncio.create_task(self._read_loop())
in a file binance/streams.py, line 98

Full description -

I did some kind of research, and what I've come to:

First I allowed debug output for websocket, which is created in file binance/streams.py, line 87

adding

logging.basicConfig(
    format="%(message)s",
    level=logging.DEBUG,
)

It allowed to verify that WS itself works fine, it receives data successfully, so seems it is a processing problem;
Processing is done by function async def _read_loop(self) of same file, so seems like it is never actually called;

I replaced
self._handle_read_loop = self._loop.call_soon_threadsafe(asyncio.create_task, self._read_loop())
to
self._handle_read_loop = asyncio.create_task(self._read_loop())
and from that moment it worked fine for me.

Now lets try to understand if this change will affect the logic - _read_loop task is created by async def connect function, which is only called by async def __aenter__(self) function;

Lets assume I want to sub to all prices update, and lets follow the flow:

market_ws_client = ThreadedWebsocketManager()
market_ws_client.daemon = True
market_ws_client.start()
market_ws_client.start_all_mark_price_socket(callback=cb)

start_all_mark_price_socket calls _start_async_socket, which creates socket by calling
socket = getattr(self._bsm, socket_name)(**params)

However, that created socket is started by calling
self._loop.call_soon_threadsafe(asyncio.create_task, self.start_listener(socket, socket_path, callback))

start_listener function calls async with socket as s which actually runs __aenter__ method mentioned above.

So actually __aenter__ is already called from inside of self._loop of ThreadedApiManager.

And self._loop should always be actually brand new event loop - please look at my other comment

Hope I haven't done any logical mistakes and hope it will help someone!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants