Skip to content

Instantly share code, notes, and snippets.

@yoadsn
Created July 21, 2025 10:21
Show Gist options
  • Select an option

  • Save yoadsn/65fc4673ca4c0c0b03260c17047b5c27 to your computer and use it in GitHub Desktop.

Select an option

Save yoadsn/65fc4673ca4c0c0b03260c17047b5c27 to your computer and use it in GitHub Desktop.
Limiting the parallel degree of Langgraph pregel task execution
max_parallel_degree = 2
check_every_secs = 1
@entrypoint
async def limited_parallel_execution(input_state: dict[any, any], config: RunnableConfig):
items = range(10)
all_tasks = []
for item in items:
t_future = process_item(item) # process_item is a @task
all_tasks.append(t_future)
print(f"Created task for {item}")
can_submit_more = False
while not can_submit_more:
_, pending_tasks = await asyncio.wait(all_tasks, timeout=check_every_secs)
if len(pending_tasks) < max_parallel_degree:
can_submit_more = True
else:
print(
"Waiting for more pending tasks to complete - current pending tasks:",
len(pending_tasks),
)
results = await asyncio.gather(*all_tasks) # Final results for all tasks
return results
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment