Encapsulate several components into one.


Encapsulate several components into one.

class Compound(config={}, config_map={}, linkages={}, **kw)[source]

Bases: RunnableNetwork

Encapsulate several components into one.

Closely modeled on Kamaelia’s ‘Graphline’ component. Components are linked within the compound and to the outside world according to the linkages parameter.

For example, you could create an image resizer by connecting a FilterGenerator to a Resize as follows:

def ImageResizer(config={}, **kwds):
    cfg = {'aperture': 16}
    return Compound(
        filgen = FilterGenerator(),
        resize = Resize(),
        config = cfg,
        config_map = {
            'up'      : ('filgen.xup',   'resize.xup',
                         'filgen.yup',   'resize.yup'),
            'down'    : ('filgen.xdown', 'resize.xdown',
                         'filgen.ydown', 'resize.ydown'),
            'aperture': ('filgen.xaperture', 'filgen.yaperture'),
        linkages = {
            ('self',   'input')  : ('resize', 'input'),
            ('filgen', 'output') : ('resize', 'filter'),
            ('resize', 'output') : ('self',    'output'),

Note the use of 'self' in the linkages parameter to denote the compound object’s own inputs and outputs. These are connected directly to the child components with no runtime overhead. There is no performance disadvantage from using compound objects. The 'self' inboxes and outboxes are added to the component’s inputs and outputs lists.

All the child components’ configuration objects are gathered into one ConfigParent. The child names are used to index the ConfigParent’s dict. This allows access to any config item in any child:

cfg = image_resizer.get_config()
cfg.filgen.xup = 3
cfg.filgen.xdown = 8
cfg.filgen.yup = 3
cfg.filgen.ydown = 8
cfg.resize.xup = 3
cfg.resize.xdown = 8
cfg.resize.yup = 3
cfg.resize.ydown = 8

Compound components to be nested to any depth whilst still making their configuration available at the top level.

The config_map allows multiple child components to be controlled by one configuration item. For each item there is a list of child config items, in parent.child form. For example, to change the scaling factor of the image resizer shown above (even while it’s running!) you might do this:

cfg = image_resizer.get_config()
cfg.up = 3
cfg.down = 8

You can also adjust the configuration when the compound component is created by passing a dict containing additional values. This allows the component’s user to over-ride the default values.

The compound component’s child components are stored in the children dict. This must not be modified but may be useful if you need to know about the component’s internals.

The component’s internal links are stored in the links list. This also must not be modified but can be used for introspection. Each element is a (src_name, outbox), (dest_name, inbox) tuple.

  • name (Component) – Add Component to the network as name. Can be repeated with different values of name.

  • linkages (dict) – A mapping from component outputs to component inputs.

  • config (dict) – Additional configuration to be applied to the components before they are connected.

  • config_map (dict) – Mapping of top level configuration names to child component configuration names.

connect(output_name, input_method)[source]

Connect an output to any callable object.

  • output_name (str) – the output to connect. Must be one of the 'self' outputs in the linkages parameter.

  • input_method (callable) – the thread-safe callable to invoke when send is called.

bind(source, dest, destmeth)[source]

Guild compatible version of connect.

This allows Pyctools compound components to be used in Guild pipelines.


See pyctools.core.config.ConfigMixin.get_config.


See pyctools.core.config.ConfigMixin.set_config.

class RunnableNetwork(linkages={}, **kw)[source]

Bases: object

Encapsulate several components into one.

This is the basic runnable network part of a Compound component.

  • name (Component) – Add Component to the network as name. Can be repeated with different values of name.

  • linkages (dict) – A mapping from component outputs to component inputs.

inputs = []
outputs = []
children = {}

Guild compatible version of start.


Start the component running.


Thread-safe method to stop the component.


Wait for the compound component’s children to stop running.


end_comps (bool) – only wait for the components that end a pipeline. This is useful for complex graphs where it is normal for some components not to terminate.

Comments or questions? Please email