Files
shy3130 697c27bb02 feat(v0.2): 市场阶段与主线识别 + 因子挖掘全链路 + 数据层完善
- 市场环境: 新增情绪周期6阶段(冰点/启动/主升/高潮/退潮/修复, 连板梯队驱动,
  EMA平滑+2日确认+弱档否决, 平均段长9.7天)与概念/行业主线排名(涨停梯队聚合,
  可配置宽基/风格标签过滤); 市场环境页重构, regime 透明加列, 与5档state并存
- 挖掘: 因子与策略挖掘全链路(API/worker/进程锁/候选库/前端工作台/文档),
  周度调度默认关闭且永不自动发布
- 回测: 财务快照因子(点时口径), 批量回测预计算共享下期收益,
  信号路径矩阵列依赖展开修复(consecutive_limit_ups 缺列报错)
- 数据/性能: enriched 生成与预热治理, 重任务限流, 行情/K线缓存复用, 时区修复
- 测试: 后端全量 914 通过; GUI 黑盒验证截图存证 gui-test-screenshots/
2026-08-16 23:39:07 +08:00

79 lines
2.0 KiB
Python

from __future__ import annotations
import os
from pathlib import Path
from typing import BinaryIO
class MiningProcessLockError(RuntimeError):
"""Another application process owns mining for this data directory."""
class MiningProcessLock:
def __init__(self, data_dir: Path) -> None:
self._path = Path(data_dir) / ".mining_process.lock"
self._stream: BinaryIO | None = None
def acquire(self) -> None:
if self._stream is not None:
return
self._path.parent.mkdir(parents=True, exist_ok=True)
stream = self._path.open("a+b")
try:
stream.seek(0, os.SEEK_END)
if stream.tell() == 0:
stream.write(b"0")
stream.flush()
os.set_inheritable(stream.fileno(), False)
_try_lock_file(stream)
except BaseException:
stream.close()
raise
self._stream = stream
def release(self) -> None:
stream = self._stream
if stream is None:
return
self._stream = None
try:
_unlock_file(stream)
finally:
stream.close()
def _try_lock_file(stream: BinaryIO) -> None:
if os.name == "nt":
import msvcrt
stream.seek(0)
try:
msvcrt.locking(stream.fileno(), msvcrt.LK_NBLCK, 1)
except OSError as exc:
raise MiningProcessLockError(
"another application process already owns mining for this data directory"
) from exc
return
import fcntl
try:
fcntl.flock(stream.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
except OSError as exc:
raise MiningProcessLockError(
"another application process already owns mining for this data directory"
) from exc
def _unlock_file(stream: BinaryIO) -> None:
if os.name == "nt":
import msvcrt
stream.seek(0)
msvcrt.locking(stream.fileno(), msvcrt.LK_UNLCK, 1)
return
import fcntl
fcntl.flock(stream.fileno(), fcntl.LOCK_UN)