1
0
mirror of https://github.com/microsoft/qlib.git synced 2026-07-11 23:06:58 +08:00

Fix Async Call (#1869)

This commit is contained in:
you-n-g
2024-12-16 18:32:46 +08:00
committed by GitHub
parent 431f574967
commit 7acb4f3484

View File

@@ -1,6 +1,7 @@
# Copyright (c) Microsoft Corporation. # Copyright (c) Microsoft Corporation.
# Licensed under the MIT License. # Licensed under the MIT License.
import threading
from functools import partial from functools import partial
from threading import Thread from threading import Thread
from typing import Callable, Text, Union from typing import Callable, Text, Union
@@ -9,7 +10,7 @@ from joblib import Parallel, delayed
from joblib._parallel_backends import MultiprocessingBackend from joblib._parallel_backends import MultiprocessingBackend
import pandas as pd import pandas as pd
from queue import Queue from queue import Empty, Queue
import concurrent import concurrent
from qlib.config import C, QlibConfig from qlib.config import C, QlibConfig
@@ -85,7 +86,17 @@ class AsyncCaller:
def run(self): def run(self):
while True: while True:
data = self._q.get() # NOTE:
# atexit will only trigger when all the threads ended. So it may results in deadlock.
# So the child-threading should actively watch the status of main threading to stop itself.
main_thread = threading.main_thread()
if not main_thread.is_alive():
break
try:
data = self._q.get(timeout=1)
except Empty:
# NOTE: avoid deadlock. make checking main thread possible
continue
if data == self.STOP_MARK: if data == self.STOP_MARK:
break break
data() data()