From c39e34c389f4940075ea34f5b4f0ca8329114d8b Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sat, 21 Mar 2026 19:56:23 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Optimize=20synchronous=20API=20exec?= =?UTF-8?q?ution=20inside=20async=20loop=20in=20macro=5Fbridge.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: aguzererler <6199053+aguzererler@users.noreply.github.com> --- tradingagents/pipeline/macro_bridge.py | 27 +++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/tradingagents/pipeline/macro_bridge.py b/tradingagents/pipeline/macro_bridge.py index 38fc5d10..42759c63 100644 --- a/tradingagents/pipeline/macro_bridge.py +++ b/tradingagents/pipeline/macro_bridge.py @@ -5,6 +5,7 @@ from __future__ import annotations import asyncio import json import logging +from concurrent.futures import ThreadPoolExecutor from tradingagents.agents.utils.json_utils import extract_json from dataclasses import dataclass from datetime import datetime @@ -242,24 +243,24 @@ async def run_all_tickers( Returns: 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() - # TradingAgentsGraph is synchronous — run it in a thread pool - return await loop.run_in_executor( - None, + loop = asyncio.get_running_loop() + executor = ThreadPoolExecutor(max_workers=max_concurrent) + try: + tasks = [ + loop.run_in_executor( + executor, run_ticker_analysis, - candidate, + c, macro_context, config, analysis_date, ) - - tasks = [_run_one(c) for c in candidates] - results = await asyncio.gather(*tasks) - return list(results) + for c in candidates + ] + results = await asyncio.gather(*tasks) + return list(results) + finally: + executor.shutdown(wait=False) # ─── Reporting ────────────────────────────────────────────────────────────────