Skip to content

Commit

Permalink
defer lock creation
Browse files Browse the repository at this point in the history
  • Loading branch information
zzstoatzz committed Jan 28, 2025
1 parent a4a567f commit 83cb350
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/prefect/server/utilities/messaging/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
logger: "logging.Logger" = get_logger(__name__)

# Simple global counters by topic with thread-safe access
_metrics_lock = asyncio.Lock()
_metrics_lock: asyncio.Lock | None = None
METRICS: dict[str, dict[str, int]] = defaultdict(
lambda: {
"published": 0,
Expand All @@ -42,6 +42,9 @@


async def update_metric(topic: str, key: str, amount: int = 1) -> None:
global _metrics_lock
if _metrics_lock is None:
_metrics_lock = asyncio.Lock()
async with _metrics_lock:
METRICS[topic][key] += amount

Expand Down

0 comments on commit 83cb350

Please sign in to comment.