Skip to content

feat(binance): enhance WebSocket functionality and data retrieval#9

Open
takltc wants to merge 2 commits into51bitquant:mainfrom
takltc:main
Open

feat(binance): enhance WebSocket functionality and data retrieval#9
takltc wants to merge 2 commits into51bitquant:mainfrom
takltc:main

Conversation

@takltc
Copy link

@takltc takltc commented Jun 10, 2025

No description provided.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self._loop: asyncio.AbstractEventLoop = get_loop() if _loop is None else _loop
self._loop: asyncio.AbstractEventLoop = None # Initialize as None, created in the run method
why? how do you test the code?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An asyncio event loop is not thread-safe. It is designed to be owned and managed by a single thread. The original code assigned the event loop in the init method, which runs in the main thread. The run() method, however, executes in a new, separate worker thread. Passing an event loop from one thread to another to be run can lead to race conditions and other unpredictable concurrency issues.

The best practice, which the new code follows, is that the thread that will run the event loop should be the one to create, manage, and eventually close it. By creating the loop inside the run() method, we ensure the new thread has its own private, isolated event loop, making the entire manager much more robust and predictable.

I added the test unit test_threaded_stream.py to test ThreadedApiManager by verifying that the thread can be started, is confirmed to be alive, and then can be stopped and cleaned up properly. Also, I simulate receiving a message from a socket and assert that the provided callback function is correctly invoked with the message content.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK,I will take a look. thanks for your PR. Best regards.

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 this pull request may close these issues.

2 participants