文件预览

subscription_registry.py

查看 paper-test2 技能包中的文件内容。

文件内容

paper_trading/subscription_registry.py

"""Engine-driven set of symbols that the feed should poll.

Maintained by Engine: add(symbol) when a LIMIT enters WORKING, remove(symbol)
when the last WORKING for that symbol is filled / cancelled / restart-cancelled
/ DAY-expired. Feed loop reads snapshot() each tick.

Thread-safe: uses a Lock because the feed loop and the engine may run
in different threads (e.g. feed-poll executor + asyncio loop), so add /
remove and snapshot can interleave from different threads. The lock is
cheap and keeps the invariants explicit; do not assume single-threaded
ownership.
"""
from __future__ import annotations
import threading


class SubscriptionRegistry:
    def __init__(self) -> None:
        self._lock = threading.Lock()
        self._symbols: set[str] = set()

    def add(self, symbol: str) -> None:
        with self._lock:
            self._symbols.add(symbol)

    def remove(self, symbol: str) -> None:
        with self._lock:
            self._symbols.discard(symbol)

    def snapshot(self) -> list[str]:
        with self._lock:
            return list(self._symbols)