Performance: Conditional routing in graphs
This affects the speed of decision-making and response time in graph-based workflows by controlling which nodes execute based on conditions.
Jump into concepts and practice - no test required
if condition: result = nodeA.run() else: result = nodeB.run() # Only one node runs based on condition
graph.add_edge('start', 'nodeA') graph.add_edge('start', 'nodeB') # Both nodes run regardless of condition resultA = nodeA.run() resultB = nodeB.run() # Condition checked after both run
| Pattern | Node Executions | Conditional Checks | Processing Time | Verdict |
|---|---|---|---|---|
| Run all nodes then check condition | All nodes run | After execution | High due to wasted runs | [X] Bad |
| Check condition before running nodes | Only needed node runs | Before execution | Lower, efficient processing | [OK] Good |
conditions = [
lambda ctx: ctx['score'] > 80,
lambda ctx: ctx['score'] > 50
]
routes = ['high', 'medium', 'low']
context = {'score': 65}
Which route will be chosen?def route_condition(context):
if context['value'] > 10:
return True
elif context['value'] < 5:
return False
routes = ['path1', 'path2']
# Routing uses route_condition