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
.
import covalent as ct
from pathlib import Path
Procedure
- Define functions to be executed before and after the electron:
def call_before_hook(filename):
Path(filename).write_text('Hello world!')
return filename
def call_after_hook(filename):
Path(filename).unlink()
Pass the functions as arguments to two Covalent dependency objects. The Covalent dependency class is
DepsCall()
.Assign the
call_before
andcall_after
named arguments in the electron decorator to reference theDepsCall
objects.DepsCall
does not intrinsically distinguish between pre- and post-execution. The only difference is which keyword you assign the argument to.
@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()
- Create and dispatch a lattice containing the electron:
@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!