Whats the best way to go about providing some kind of global context to all nodes, say I'm processing a file and I have an object to represent the file, with attributes etc, and I want to be able to access that in every node?
1 Answer
There are a lot of options depending on what you mean.
This is something you can achieve using regular python software development practices (see the class example below).
First option, if it's «global», is to use a global variable. Ok, that's not the best practice in the world, but I think this is bad mostly because you rarely want a global state (or you should rarely want it).
Now that the obvious is probably crossed out, let's think about what "global context" means in your question.
If it means something "global" to the execution, and defined outside of it, you can group this configuration object in a service and inject it into the nodes that need it.
config = {"location": "at joe"}
@use("config")
def choose_a_place_to_eat(config):
return config["location"]
bonobo.run(..., services={"config": config})
If this is something that concerns a bunch of nodes, you can also group those nodes in a class whose role is to keep this configuration state.
class Venue():
def __init__(self, name):
self.name = name
def producer(self):
return self.name
def checker(self, name):
return self.name == name
venue = Venue(name="Joe")
graph = bonobo.Graph(venue.producer, venue.checker, print)
bonobo.run(graph)
or even (replacing the last part of the previous example)...
def build_graph(venue):
return bonobo.Graph(venue.producer, venue.checker, print)
bonobo.run(build_graph(Venue(name="Joe")))
Bonobo internally also keeps a context for nodes, and graphs, related to one execution, and you can use those objects if you're familiar with threads (execution order is not predictible, so be careful about that). There are plans a few releases from now to add threadsafe tooling for this in the graph execution context.
Those contexts are created when the execution starts, and returned when execution finishes.
Hope that help.