Merge pull request #72 from aguzererler/fix-macro-bridge-concurrency-3956784745339794663

 Optimize synchronous API execution concurrency in macro_bridge.py
This commit is contained in:
ahmet guzererler 2026-03-21 22:58:14 +01:00 committed by GitHub
commit 28f35a54ed
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 14 additions and 13 deletions

View File

@ -5,6 +5,7 @@ from __future__ import annotations
import asyncio import asyncio
import json import json
import logging import logging
from concurrent.futures import ThreadPoolExecutor
from tradingagents.agents.utils.json_utils import extract_json from tradingagents.agents.utils.json_utils import extract_json
from dataclasses import dataclass from dataclasses import dataclass
from datetime import datetime from datetime import datetime
@ -242,24 +243,24 @@ async def run_all_tickers(
Returns: Returns:
List of TickerResult in completion order. List of TickerResult in completion order.
""" """
semaphore = asyncio.Semaphore(max_concurrent)
async def _run_one(candidate: StockCandidate) -> TickerResult:
async with semaphore:
loop = asyncio.get_running_loop() loop = asyncio.get_running_loop()
# TradingAgentsGraph is synchronous — run it in a thread pool executor = ThreadPoolExecutor(max_workers=max_concurrent)
return await loop.run_in_executor( try:
None, tasks = [
loop.run_in_executor(
executor,
run_ticker_analysis, run_ticker_analysis,
candidate, c,
macro_context, macro_context,
config, config,
analysis_date, analysis_date,
) )
for c in candidates
tasks = [_run_one(c) for c in candidates] ]
results = await asyncio.gather(*tasks) results = await asyncio.gather(*tasks)
return list(results) return list(results)
finally:
executor.shutdown(wait=False)
# ─── Reporting ──────────────────────────────────────────────────────────────── # ─── Reporting ────────────────────────────────────────────────────────────────