feat(binance): enhance WebSocket functionality and data retrieval#9
feat(binance): enhance WebSocket functionality and data retrieval#9takltc wants to merge 2 commits into51bitquant:mainfrom
Conversation
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
OK,I will take a look. thanks for your PR. Best regards.
No description provided.