Context Object
  • 13 Jul 2022
  • 1 Minute to read
  • Contributors
  • Dark
    Light
  • PDF

Context Object

  • Dark
    Light
  • PDF

Article summary

Certain functions that you provide to Nodes have a method signature that has context as a keyword argument. These functions include bitmappers and processors, and can be included in you Nodes either by coding them in the Node itself (normally starting with a template from the received MessageType) or by referring to a Function from the Function Library in your Tenant. Whether you code the function directly in the Node, or reference it from the Function library, we will refer to it within this section of documentation simply as your function.

The context keyword argument references a Context object that EchoStream provides when it calls your function.

This Context object has the following characteristics:

  • It survives across executions of your functions while the Node that your function is attached to is "in memory". This allows you to maintain state across such executions.
  • It is a Lock object, allowing you control access to the Context object during concurrent executions of your function.
  • It may be extended by adding attributes to the Context object in addition to the attributes that are provided by EchoStream.

Type hinting

If you wish to have type hints when working with the Context object, you may add the echostream-function-context package to your Function or Node requirements, and then cast the context argument to a Context object.

def processor(*, context, message, source, **kwargs):

    from typing import TYPE_CHECKING, cast

    if TYPE_CHECKING:
        from echostream_function_context import Context
    else:
        Context = object

    context = cast(Context, context)