m4us.core.coroutines

Provides support for using regular Python coroutines.

Members

coroutine(lazy=True)

Decorator factory to automatically activate Python coroutines.

Before a Python coroutine can be used, it needs to be activated by sending None as it's first message. This decorator does this automatically.

Coroutines are presumptively lazy. This decorator can flag the given coroutine as not lazy by making it adaptable to the INotLazy marker interface.

This decorator also registers the function as providing ICoroutineFactory.

Parameters:
Returns:

A wrapper function that will activate and return the coroutine when called.

Return type:

FunctionType

null_sink()

Swallow all messages except IShutdown.

This coroutine can serve as an end point in a series of connected coroutines. All messages sent to it, except IShutdown messages are ignored and not re-emitted.

The coroutine will shutdown on any IShutdown message, forwarding it on before quitting.

Returns:A null sink coroutine
Return type:GeneratorType
Implements :ICoroutine
Provides :ICoroutineFactory
sample_coroutine()

Pass all messages through.

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

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 coroutine is:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
@coroutine()
def sample_coroutine():
    """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)
Returns:A pass-through coroutine
Return type:GeneratorType
Implements :ICoroutine
Provides :ICoroutineFactory

Note

Producers and filters need a minimum of 2 yield statements as the output of the first one is always thrown away. The output of the second one is the first message delivered. On the other hand, the first yield will be the one that gets the first incoming message.

Table Of Contents

Previous topic

m4us.core.containers

Next topic

m4us.core.exceptions

This Page