Skip to content

Commit

Permalink
[Mod] 改为只有当事件循环尚未初始化的时候,才去做创建
Browse files Browse the repository at this point in the history
  • Loading branch information
vnpy committed Jun 20, 2021
1 parent aa1ff38 commit 8073a51
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions vnpy_websocket/websocket_client.py
Expand Up @@ -70,7 +70,11 @@ def start(self):
请等待on_connected被调用后,再发送数据包。
"""
self._active = True
self._loop = start_event_loop()

if not self._loop:
self._loop = get_event_loop()
start_event_loop(self._loop)

run_coroutine_threadsafe(self._run(), self._loop)

def stop(self):
Expand Down Expand Up @@ -191,18 +195,14 @@ def _record_last_received_text(self, text: str):
self._last_received_text = text[:1000]


def start_event_loop() -> AbstractEventLoop:
def start_event_loop(loop: AbstractEventLoop) -> AbstractEventLoop:
"""启动事件循环"""
loop: AbstractEventLoop = get_event_loop()

# 如果事件循环未运行,则创建后台线程来运行
if not loop.is_running():
thread = Thread(target=run_event_loop, args=(loop,))
thread.daemon = True
thread.start()

return loop


def run_event_loop(loop: AbstractEventLoop) -> None:
"""运行事件循环"""
Expand Down

0 comments on commit 8073a51

Please sign in to comment.