Adding Callable Function Dependencies to an Electron#

Add functions to be invoked before or after executing an electron by using the electron’s call_before and call_after key parameters. The functions, or dependency calls, are run in the electron’s execution environment and can be used, for example, to set up and tear down resources for the electron’s use.

Prerequisites#

Import covalent and Path.

[5]:
import covalent as ct
from pathlib import Path

Procedure#

  1. Define functions to be executed before and after the electron:

[6]:
def call_before_hook(filename):
    Path(filename).write_text('Hello world!')
    return filename

def call_after_hook(filename):
    Path(filename).unlink()
  1. Pass the functions as arguments to two Covalent dependency objects. The Covalent dependency class is DepsCall().

  2. Assign the call_before and call_after named arguments in the electron decorator to reference the DepsCall objects.

    DepsCall does not intrinsically distinguish between pre- and post-execution. The only difference is which keyword you assign the argument to.

[7]:
@ct.electron(
    call_before=ct.DepsCall(call_before_hook, args=('test.txt',), retval_keyword='my_file'),
    call_after=ct.DepsCall(call_after_hook, args=('test.txt',)),
)
def read_from_file(my_file=None):
    with open(my_file,'r') as f:
        return f.read()
  1. Create and dispatch a lattice containing the electron:

[8]:
@ct.lattice
def workflow():
    return read_from_file()

dispatch_id = ct.dispatch(workflow)()
r = ct.get_result(dispatch_id, wait=True)
print(r.result)
Hello world!

See Also#

Adding Pip Dependencies to an Electron

Adding Bash Dependencies to an Electron