m4us.core.components

Provides various kinds of Component classes.

Components are coroutines with context. That is, they are classes that implement ICoroutine, but can also store and access instance attributes.

Inheritance Diagram

Inheritance diagram of m4us.core.components

Members

class Component(**kwargs)[source]

Bases: object

Base class for components.

Components act like coroutines but can store and retrieve context via instance attributes. This corresponds in concept to Kamaelia's Component class.

Components have an internal coroutine defined in the _main() method, which sub-classes should implement.

Parameters:kwargs (collections.Mapping) -- Any keyword arguments given at instantiation are automatically set as instance attributes for convenience.
Implements :m4us.core.interfaces.ICoroutine
Provides :m4us.core.interfaces.ICoroutineFactory
_main()[source]

The component's coroutine.

Sub-classes are expected to override this method. It should be a coroutine which will automatically be activated upon instantiation.

This coroutine should work just like regular coroutines, but has access to self and any context that the instance contains.

close()[source]

Terminate the component.

This method just calls close() on the component's internal coroutine defined in _main().

See also

The ICoroutine interface for details about this method.

send(message)[source]

Send the message to the component.

This method just passes the message on to the internal coroutine defined in _main(), returning any resulting message.

See also

The ICoroutine interface for details about this method.

throw(exception)[source]

Send the exception to the component.

This method just passes the exception on to the internal coroutine defined in _main(), returning any resulting message or raising the uncaught exception.

See also

The ICoroutine interface for details about this method.

class SampleComponent(**kwargs)[source]

Bases: m4us.core.components.Component

Component that passes all messages through.

This component is meant to provide a canonical example of what a component used with this project looks like.

Like sample_coroutine(), any messages sent to it on any inbox will be sent back out on it's outbox outbox. It is also well behaved in that it will shutdown on any IShutdown message, forwarding it on before quitting.

The full code for this component is:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
@provider(ICoroutineFactory)
class SampleComponent(Component):

    """Component that passes all messages through."""

    def _main(self):
        """Pass all messages through."""
        inbox, message = (yield)
        while True:
            if is_shutdown(inbox, message):
                yield 'signal', message
                break
            ## Your code goes here.
            inbox, message = (yield 'outbox', message)
Implements :m4us.core.interfaces.ICoroutine
Provides :m4us.core.interfaces.ICoroutineFactory

See also

The sample_coroutine() coroutine for details on why the _main() method is written the way it is.

Table Of Contents

Previous topic

m4us.core.api

Next topic

m4us.core.containers

This Page