Files
tick-stock-panel/backend/app/backtest/numba_runtime.py
T
CJohn 33921fcc51 fix: 串行化 Numba parallel 内核,避免策略页并发崩溃
并发 run_all 会触发 workqueue Concurrent access,导致后端进程中断。
为 matrix parallel 内核加进程锁,前端对 run_all 做 pending 去重。
2026-07-26 11:56:10 +08:00

24 lines
702 B
Python

"""Serialize Numba ``@njit(parallel=True)`` kernels across request threads.
Numba's default ``workqueue`` threading layer is not thread-safe. Concurrent
calls from FastAPI worker threads terminate the process
(``Concurrent access has been detected`` → socket hang up).
A process-wide lock is enough: overlapping kernels queue instead of crashing.
"""
from __future__ import annotations
import threading
from collections.abc import Callable
from typing import TypeVar
_NUMBA_PARALLEL_LOCK = threading.RLock()
T = TypeVar("T")
def run_numba_parallel(fn: Callable[[], T]) -> T:
"""Run a Numba parallel kernel under the process-wide lock."""
with _NUMBA_PARALLEL_LOCK:
return fn()