python-binance icon indicating copy to clipboard operation
python-binance copied to clipboard

Fixed event loop creation issue in threaded_stream.py file

Open alkhachatryan opened this issue 2 years ago • 1 comments

Fixed event loop creation issue in threaded_stream.py file by simple checking if the loop is already created or not. If is not created - create a new one, otherwise get current one

alkhachatryan avatar Mar 07 '24 15:03 alkhachatryan

@sammchardy this is very simple fix to a very critical bug which stopped a lof of projects and blocked them. At least one we have here: https://github.com/conor19w/Binance-Futures-Trading-Bot/issues/61

Merge it as soon as possible. Thanks.

alkhachatryan avatar Mar 07 '24 15:03 alkhachatryan

Thanks @alkhachatryan

sammchardy avatar Aug 14 '24 06:08 sammchardy

you 2

alkhachatryan avatar Aug 14 '24 16:08 alkhachatryan

@alkhachatryan Hello, I'm afraid your fix broke something because with it, the code just hangs (using python3.12 now)

Example

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(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)

    streams = ["bnbbtc@miniTicker", "bnbbtc@bookTicker"]
    twm.start_multiplex_socket(callback=handle_socket_message, streams=streams)

    twm.join()


main()

carlosmiei avatar Oct 24 '24 09:10 carlosmiei

@carlosmiei hey. Strange, I didn't get that issue in ubuntu(intel), raspbian(amd) and VM ubuntu on AWS. If you sure it's my fix, please provide more info, if needed we can have a call to find out the problem

alkhachatryan avatar Oct 24 '24 10:10 alkhachatryan

@alkhachatryan Can you please run the snippet above and report back the result? Right now have multiple reports (check the linked PR) that adding asyncio.get_event_loop() if asyncio.get_event_loop().is_running() else asyncio.new_event_loop() freezes the code execution.

carlosmiei avatar Oct 24 '24 11:10 carlosmiei

@alkhachatryan Also I think that asyncio.get_event_loop().is_running() is not the same thing as there's no event loop in the thread. 🤔

carlosmiei avatar Oct 24 '24 11:10 carlosmiei

Hi @alkhachatryan , do you have an example that caused an issue with this code?

pcriadoperez avatar Oct 24 '24 11:10 pcriadoperez

@pcriadoperez https://github.com/conor19w/Binance-Futures-Trading-Bot/issues/61 here you can see the backtrace

alkhachatryan avatar Oct 25 '24 09:10 alkhachatryan

@pcriadoperez and @carlosmiei let's continue the discussion in PR https://github.com/sammchardy/python-binance/pull/1449

alkhachatryan avatar Oct 25 '24 09:10 alkhachatryan