Skip to content
/

Wait for both

PathsChapter 7 of 154 min read

To check an invoice, the desk needs what the invoice asks and what the contract says, so this chapter answers: how does a step wait for two results?


Two steps read one fact each, and the check needs both:

Drawing diagram…
InputToolResultThe check needs two facts
  • and_(): runs a step once all the steps it lists have finished.

Set up#

If you closed the terminal, go back into the folder and switch the environment on:

Terminal
$ cd document-desk
$ source env/bin/activate

Way 1: one after the other#

read_terms waits for read_invoice, and the check waits for read_terms, so the check runs after both. time.sleep(1) stands in for a reading that takes one second, and the check reports how long the run took. Let's save this as demo.py:

demo.py
# demo.py
import time

from crewai.flow.flow import Flow, listen, start
from pydantic import BaseModel, ConfigDict


class DeskState(BaseModel):
    model_config = ConfigDict(validate_assignment=True)

    began: float = 0.0
    amount: int = 0
    price: int = 0


class Desk(Flow[DeskState]):
    suppress_flow_events = True

    @start()
    def read_document(self):
        self.state.began = time.time()

    @listen(read_document)
    def read_invoice(self):
        time.sleep(1)
        self.state.amount = 19260

    @listen(read_invoice)
    def read_terms(self):
        time.sleep(1)
        self.state.price = 18000

    @listen(read_terms)
    def check_invoice(self):
        seconds = round(time.time() - self.state.began)
        over = self.state.amount - self.state.price
        return f"the invoice is {over} over the contract ({seconds} s)"


print(Desk().kickoff())
Terminal
(env)$ python demo.py
Output
the invoice is 1260 over the contract (2 s)

The check is right, but read_terms waited a second for read_invoice, though it needs nothing from it:

Drawing diagram…
InputToolResultOne after the other: 2 seconds

Way 2: both at once, then the check#

Both readings listen to read_document, so they start together, and and_() holds the check until both have finished. Add and_ to the import in demo.py:

Python
from crewai.flow.flow import Flow, and_, listen, start

Change read_terms and check_invoice in demo.py:

Python
    @listen(read_document)
    def read_terms(self):
        time.sleep(1)
        self.state.price = 18000

    @listen(and_(read_invoice, read_terms))
    def check_invoice(self):
        seconds = round(time.time() - self.state.began)
        over = self.state.amount - self.state.price
        return f"the invoice is {over} over the contract ({seconds} s)"
Terminal
(env)$ python demo.py
Output
the invoice is 1260 over the contract (1 s)

The same answer in half the time: CrewAI ran the two readings side by side.

Drawing diagram…
InputToolResultBoth at once: 1 second

Each reading puts its fact in the state, so the check reads both from there.

When one side fails#

Let's see what the check does when the contract can't be read. Change read_terms in demo.py:

Python
    @listen(read_document)
    def read_terms(self):
        time.sleep(1)
        raise FileNotFoundError("the contract is missing")
Terminal
(env)$ python demo.py
Output
Error executing listener read_terms: the contract is missingTraceback (most recent call last):...FileNotFoundError: the contract is missing

and_() never saw read_terms finish, so the check never ran, and kickoff() raised the error:

Drawing diagram…
InputToolResultOne side fails, the check never runs

Let's remove the program. The folder stays for the next chapter.

Terminal
(env)$ rm demo.py

The two ways side by side#

Compare how long the check waits:

Drawing diagram…
InputResultHow long the check waits

When a step needs two results that don't depend on each other, start both together and join them with and_().

Drawing diagram…
InputResultand_(), or_(), or one after the other?

What comes next#

Every step so far is our own code, with the facts written in. On the real desk, an AI reads the invoice:

Drawing diagram…
InputAgentToolAn AI in a step

So how does an agent become one step of the flow?