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:
commit
28f35a54ed
|
|
@ -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)
|
loop = asyncio.get_running_loop()
|
||||||
|
executor = ThreadPoolExecutor(max_workers=max_concurrent)
|
||||||
async def _run_one(candidate: StockCandidate) -> TickerResult:
|
try:
|
||||||
async with semaphore:
|
tasks = [
|
||||||
loop = asyncio.get_running_loop()
|
loop.run_in_executor(
|
||||||
# TradingAgentsGraph is synchronous — run it in a thread pool
|
executor,
|
||||||
return await loop.run_in_executor(
|
|
||||||
None,
|
|
||||||
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 ────────────────────────────────────────────────────────────────
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue