Switch parallel research/risk to sync ThreadPoolExecutor with timing logs

Use sync functions with pool.submit() instead of async+run_in_executor
to avoid potential asyncio event-loop interaction issues with LangGraph.
Added timing logs to diagnose parallelism.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
dtarkent2-sys 2026-02-20 15:39:25 +00:00
parent 7ff05328a8
commit 2484bd89e4
1 changed files with 83 additions and 38 deletions

View File

@ -83,31 +83,62 @@ def create_parallel_analyst_node(analyst_fns, tool_nodes, selected_analysts):
return parallel_analysts_node return parallel_analysts_node
def _snapshot_research_state(state):
"""Extract research-relevant fields into a plain dict."""
return {
"investment_debate_state": dict(state.get("investment_debate_state", {})),
"market_report": state.get("market_report", ""),
"sentiment_report": state.get("sentiment_report", ""),
"news_report": state.get("news_report", ""),
"fundamentals_report": state.get("fundamentals_report", ""),
}
def _snapshot_risk_state(state):
"""Extract risk-relevant fields into a plain dict."""
return {
"risk_debate_state": dict(state.get("risk_debate_state", {})),
"market_report": state.get("market_report", ""),
"sentiment_report": state.get("sentiment_report", ""),
"news_report": state.get("news_report", ""),
"fundamentals_report": state.get("fundamentals_report", ""),
"trader_investment_plan": state.get("trader_investment_plan", ""),
}
def create_parallel_research_node(bull_fn, bear_fn): def create_parallel_research_node(bull_fn, bear_fn):
"""Create a node that runs Bull and Bear researchers in parallel. """Create a node that runs Bull and Bear researchers in parallel.
Both agents receive the same state (reports + empty debate state) and Uses a sync function with ThreadPoolExecutor.submit() to avoid any
produce independent arguments. Results are merged into a single asyncio event-loop interaction issues. LangGraph handles running sync
investment_debate_state with both histories and count=2. nodes in its own thread, and from there we spawn our own pool.
""" """
async def parallel_research_node(state): def parallel_research_node(state):
# Snapshot into plain dicts — LangGraph state proxies serialize import time
# concurrent dict access, which would force sequential execution.
state_snap = { state_snap = _snapshot_research_state(state)
"investment_debate_state": dict(state.get("investment_debate_state", {})), t0 = time.time()
"market_report": state.get("market_report", ""),
"sentiment_report": state.get("sentiment_report", ""), def run_bull():
"news_report": state.get("news_report", ""), logger.info("Bull researcher starting")
"fundamentals_report": state.get("fundamentals_report", ""), result = bull_fn(state_snap)
} logger.info("Bull researcher done in %.1fs", time.time() - t0)
return result
def run_bear():
logger.info("Bear researcher starting")
result = bear_fn(state_snap)
logger.info("Bear researcher done in %.1fs", time.time() - t0)
return result
loop = asyncio.get_running_loop()
with ThreadPoolExecutor(max_workers=2) as pool: with ThreadPoolExecutor(max_workers=2) as pool:
bull_result, bear_result = await asyncio.gather( bull_future = pool.submit(run_bull)
loop.run_in_executor(pool, bull_fn, state_snap), bear_future = pool.submit(run_bear)
loop.run_in_executor(pool, bear_fn, state_snap), bull_result = bull_future.result()
) bear_result = bear_future.result()
logger.info("Parallel research total: %.1fs", time.time() - t0)
bull_debate = bull_result["investment_debate_state"] bull_debate = bull_result["investment_debate_state"]
bear_debate = bear_result["investment_debate_state"] bear_debate = bear_result["investment_debate_state"]
@ -130,30 +161,44 @@ def create_parallel_research_node(bull_fn, bear_fn):
def create_parallel_risk_node(aggressive_fn, conservative_fn, neutral_fn): def create_parallel_risk_node(aggressive_fn, conservative_fn, neutral_fn):
"""Create a node that runs all 3 risk analysts in parallel. """Create a node that runs all 3 risk analysts in parallel.
All agents receive the same state (trader plan + empty risk debate state) Uses a sync function with ThreadPoolExecutor.submit() to avoid any
and produce independent arguments. Results are merged into a single asyncio event-loop interaction issues. LangGraph handles running sync
risk_debate_state with all histories and count=3. nodes in its own thread, and from there we spawn our own pool.
""" """
async def parallel_risk_node(state): def parallel_risk_node(state):
# Snapshot into plain dicts — LangGraph state proxies serialize import time
# concurrent dict access, which would force sequential execution.
state_snap = { state_snap = _snapshot_risk_state(state)
"risk_debate_state": dict(state.get("risk_debate_state", {})), t0 = time.time()
"market_report": state.get("market_report", ""),
"sentiment_report": state.get("sentiment_report", ""), def run_agg():
"news_report": state.get("news_report", ""), logger.info("Aggressive analyst starting")
"fundamentals_report": state.get("fundamentals_report", ""), result = aggressive_fn(state_snap)
"trader_investment_plan": state.get("trader_investment_plan", ""), logger.info("Aggressive analyst done in %.1fs", time.time() - t0)
} return result
def run_con():
logger.info("Conservative analyst starting")
result = conservative_fn(state_snap)
logger.info("Conservative analyst done in %.1fs", time.time() - t0)
return result
def run_neu():
logger.info("Neutral analyst starting")
result = neutral_fn(state_snap)
logger.info("Neutral analyst done in %.1fs", time.time() - t0)
return result
loop = asyncio.get_running_loop()
with ThreadPoolExecutor(max_workers=3) as pool: with ThreadPoolExecutor(max_workers=3) as pool:
agg_result, con_result, neu_result = await asyncio.gather( agg_future = pool.submit(run_agg)
loop.run_in_executor(pool, aggressive_fn, state_snap), con_future = pool.submit(run_con)
loop.run_in_executor(pool, conservative_fn, state_snap), neu_future = pool.submit(run_neu)
loop.run_in_executor(pool, neutral_fn, state_snap), agg_result = agg_future.result()
) con_result = con_future.result()
neu_result = neu_future.result()
logger.info("Parallel risk total: %.1fs", time.time() - t0)
agg_debate = agg_result["risk_debate_state"] agg_debate = agg_result["risk_debate_state"]
con_debate = con_result["risk_debate_state"] con_debate = con_result["risk_debate_state"]