[lttng-dev] bt2 python - accessing component instances added to graph

Simon Marchi simark at simark.ca
Mon Mar 30 09:45:30 EDT 2020

On 2020-03-30 1:24 a.m., Rocky Dunlap via lttng-dev wrote:
> I noticed that when I add a component to a bt2 graph, e.g.:
> sink = g.add_component(SinglePETSink, "sink"+str(idx))
> The return value "sink" is not actually an instance of SinglePETSink (which has been defined as a local Python class).  How do I get to the instance of SinglePETSink that was created?

If I understand your question correctly: this is actually on purpose to preserve
encapsulation of each component: the user of the graph has the control on the
component when creating it, it can pass parameters to it.  But after that, it
should not be able to go play with the internals of the component.  The `self`
object that has access to the component's state is only available when the
component's methods are called.

So when the user of the graph adds a component, it only receives a generic
component object, which can only be used to access public properties (including
a public view of its ports, that you can use to connect them).

Of course, you can always get around this with a global variable, doing like
this in the component's init:

  global my_component
  my_component = self

But hopefully you won't need to.  If you explain what you are trying to achieve,
we can see if there's a proper way to do it using the existing API.


More information about the lttng-dev mailing list