Skip to main content

Adding Constraints to Tasks and Workflows GoToimage

Add constraints and execution directives to electrons by specifying them as arguments to the Covalent @electron decorator.

The most-used constraint is to explicitly name an executor for an electron. Other constraints are described in the API Reference.

Prerequisites

Create a lattice.

import covalent as ct

@ct.electron
def sum(x, y):
return x

@ct.electron
def square(x):
return x * x

@ct.lattice
def sum_of_squares_wf(a, b):
x2 = square(x=a)
y2 = square(y=b)
return sum(x=x2, y=y2)

Procedure

  1. To specify a constraint on a single electron, add the constraint as a keyword argument to its Covalent @electron decorator.
@ct.electron(executor = "local")
def sum(x, y):
return x
  1. To specify a constraint on all electrons in a lattice, add the constraint as a keyword argument to the @lattice decorator. The constraint is inherited by all electrons in the lattice.
@ct.lattice(executor = "local")
def sum_of_squares_wf(a, b):
x2 = square(x=a)
y2 = square(y=b)
return sum(x=x2, y=y2)
  1. To override a constraint inherited from a lattice, specify the overriding constraint on the @electron decorator.
import covalent as ct

@ct.electron(executor = "dask")
def sum(x, y):
return x

@ct.electron
def square(x):
return x * x

@ct.lattice(executor = "local")
def sum_of_squares_wf(a, b):
x2 = square(x=a)
y2 = square(y=b)
return sum(x=x2, y=y2)

In the example above, the square electron inherits the local executor from the lattice. The sum electron overrides the inherited value and uses the dask executor instead.

See Also

Adding Pip Dependencies to an Electron

Adding Bash Dependencies to an Electron

Adding Callable Function Dependencies to an Electron