RunnableParallel helps run multiple tasks at the same time. This makes programs faster by doing many things together instead of one after another.
0
0
Parallel execution with RunnableParallel in LangChain
Introduction
When you want to ask several questions to different chatbots at once.
When you need to process multiple pieces of data at the same time.
When you want to speed up tasks that don't depend on each other.
When you want to combine results from different tools quickly.
Syntax
LangChain
from langchain.schema.runnable import RunnableParallel parallel_runner = RunnableParallel({"r1": runnable1, "r2": runnable2, ...}) result = parallel_runner.invoke(input_data)
You create RunnableParallel by giving it a dict of keys to tasks (runnables) to run together.
Calling invoke runs all tasks at once and returns their results as a dict.
Examples
This runs two simple tasks in parallel and gets their outputs as a dict.
LangChain
from langchain.schema.runnable import RunnableParallel, RunnableLambda r1 = RunnableLambda(lambda x: 'Hello') r2 = RunnableLambda(lambda x: 'World') parallel = RunnableParallel({"r1": r1, "r2": r2}) output = parallel.invoke(None)
Runs three tasks at the same time using the same input data.
LangChain
parallel = RunnableParallel({"a": runnableA, "b": runnableB, "c": runnableC})
results = parallel.invoke(input_data)Sample Program
This example shows how to run two simple tasks at the same time and print their results. Each task just returns a message.
LangChain
from langchain.schema.runnable import RunnableParallel, RunnableLambda # Define two simple runnables that return fixed strings r1 = RunnableLambda(lambda x: 'Task 1 done') r2 = RunnableLambda(lambda x: 'Task 2 done') # Create a RunnableParallel to run both at the same time parallel_runner = RunnableParallel({"r1": r1, "r2": r2}) # Run the tasks in parallel results = parallel_runner.invoke(None) # Print the results for i, (key, res) in enumerate(results.items(), 1): print(f"Result from task {i}: {res}")
OutputSuccess
Important Notes
RunnableParallel runs tasks at the same time, so tasks should not depend on each other.
Results come back in the same order as the tasks were given (Python 3.7+ dict insertion order).
Use this to speed up independent tasks and improve performance.
Summary
RunnableParallel runs multiple tasks together to save time.
It returns a dict of results matching the keys of tasks.
Great for tasks that can happen at the same time without waiting.