mirror of
https://ghfast.top/https://github.com/aeroxw/tick-stock-panel.git
synced 2026-09-12 17:54:15 +08:00
并发 run_all 会触发 workqueue Concurrent access,导致后端进程中断。 为 matrix parallel 内核加进程锁,前端对 run_all 做 pending 去重。
24 lines
702 B
Python
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()
|