Skip to content

Shared variables

Hoai Phuoc Truong edited this page Jun 28, 2020 · 7 revisions

Shared variables

As mentioned in Getting started, shared variables section, shared variables can be read by all tasks and written to by all tasks. This allows us to pass data between tasks, and invoke one task from another.

By combining with the CLI, we can pass the execution context (and related data) to the task being invoked by setting the value of the variable just before we invoke the task.

Shared variable operations

There are three main operations on shared variables:

Set value of a variable

Setting a variable assigns certain value to a shared variable. Shared variables can only store string as their values. The new value may be the same as the existing value. This assignment has two side effects:

  1. This will invoke task(s) that have/has their activation(s) set to include this variable.
  2. This will notify all tasks and processes that are waiting on this variable.

Read value of a variable

This reads the current value of the variable. This value may be null (or None in Python) if the variable is uninitialized.

Wait for a variable

Waiting for a variable is essentially waiting for a new assignment of the variable's value. This is a blocking operation. If there is a new assignment to the value of the variable, the waiter will be notified and code will continue its execution. Note that this happens even if the new value is the same as the old value.

If the wait times out, a null (or None in Python) value will be returned as the new value of the variable.

Namespace

If there are a very limited amount of shared variables, using the default "global" namespace is fine. However, when there is a large amount of shared variables, it is useful to segment variables by namespace for better management. Each namespace can define a logical group of variables (for example, all variables that belong to a task, or a group of tasks). In addition to invoking a task by changing a single variable, a task activation can be specified so that the task is invoked on changes to any variable within a namespace, or even all variables globally (though less recommended).