0
0
LangChainframework~30 mins

Parallel execution with RunnableParallel in LangChain - Mini Project: Build & Apply

Choose your learning style9 modes available
Parallel execution with RunnableParallel
📖 Scenario: You are building a simple program that runs two tasks at the same time to save time. Each task is a small function that returns a message. You will use RunnableParallel from LangChain to run these tasks together.
🎯 Goal: Create two simple functions that return messages, then use RunnableParallel to run them at the same time and get both results.
📋 What You'll Learn
Create two functions called task1 and task2 that return strings
Create a RunnableParallel instance with task1 and task2
Run the parallel tasks and store the results in a variable called results
Print the results to see both outputs
💡 Why This Matters
🌍 Real World
Running multiple tasks at the same time can save time in programs that do independent work, like fetching data from different sources or processing files.
💼 Career
Understanding parallel execution is important for building efficient applications and services that handle multiple operations simultaneously.
Progress0 / 4 steps
1
Create two simple functions
Create two functions called task1 and task2. task1 should return the string 'Hello from task 1'. task2 should return the string 'Hello from task 2'.
LangChain
Need a hint?

Use def to create functions and return to send back the message.

2
Import RunnableParallel and create instance
Import RunnableParallel from langchain.runnables. Then create a variable called parallel_runner and set it to RunnableParallel({"task1": task1, "task2": task2}).
LangChain
Need a hint?

Use the import statement exactly as shown. Pass a dictionary {"task1": task1, "task2": task2} to RunnableParallel.

3
Run the parallel tasks
Use the invoke method of parallel_runner to run both tasks at the same time. Store the result in a variable called results.
LangChain
Need a hint?

Call parallel_runner.invoke({}) and assign it to results.

4
Print the results
Add a line to print the results variable to see the output of both tasks.
LangChain
Need a hint?

Use print(results) to show the output.