LUCIT-Systems-and-Development / unicorn-binance-websocket-api

A Python SDK by LUCIT to use the Binance Websocket API`s (com+testnet, com-margin+testnet, com-isolated_margin+testnet, com-futures+testnet, com-coin_futures, us, tr, dex/chain+testnet) in a simple, fast, flexible, robust and fully-featured way.
https://unicorn-binance-websocket-api.docs.lucit.tech/
Other
677 stars 166 forks source link

replace_stream does not work correctly #225

Closed dima-dmytruk23 closed 2 years ago

dima-dmytruk23 commented 2 years ago

I have 3 binance accounts. For each account, I create instance of BinanceWebSocketApiManager and a stream for each.

    def start_websockets(self):
        for account in BinanceAccount.all():
            websocket = BinanceWebSocketApiManager(
                exchange="binance.com-futures",
                warn_on_update=False,
            )
            stream = websocket.create_stream(
                ("arr",),
                ("!userData",),
                api_key=account.api_key,
                api_secret=account.api_secret,
            )
            self.websockets[account] = [websocket, stream]

After a certain time, I want to replace streams. But, I observe that the number of threads is constantly increasing by 6.

    def toggle_websockets(self):
        for account, websocket in self.websockets.items():
            old_stream = websocket[1]
            stream = websocket[0].replace_stream(
                stream_id=old_stream,
                new_channels=("arr",),
                new_markets=("!userData",),
                new_api_key=account.api_key,
                new_api_secret=account.api_secret,
            )
            if stream:
                print(f"{stream=}, {old_stream=}")
                try:
                    websocket[1] = stream
                    websocket[0].stop_stream(old_stream)
                    websocket[0].delete_stream_from_stream_list(old_stream)
                except Exception as e:
                    print(e)

I use unicorn-binance-websocket-api==1.34.2

oliver-zehentleitner commented 2 years ago

Hi!

  1. You can use one ubwa instance for 3 different accounts (makes life easier)
  2. replace is "create a new stream", then "wait till first record is received", then "stop old stream" - on a user data stream with no action this can take a while :/
  3. one stream is using 3 threads i think. if you restart 3 streams then you maybe get 6 threads more then you expected? but they should get closed as soon the old streams get closed...

best regards

oliver-zehentleitner commented 2 years ago

just reopen if needed