Workflows

This section describes the different classes related to workflows, workfunctions and WorkChains.

class aiida.work.persistence.Persistence(running_directory='/tmp/running', finished_directory='/tmp/running/finished', failed_directory='/tmp/running/failed')[source]

Class that uses pickles stored in particular directories to persist the instance state of Processes.

classmethod create_from_basedir(basedir, **kwargs)[source]
Create using a base directory, the pickles will be stored in:
  • running: [basedir]/running
  • finished: [basedir]/finished
  • failed: [basedir]/failed
Parameters:
  • basedir (str) – The base directory to storage pickle under
  • kwargs – Any additional arguments to pass to the constructor
Returns:

A new instance.

create_from_file_and_persist(filepath)[source]

Try and load a process from a file and recreate the Process instance. To prevent multiple threads from recreating a Process from the same pickle, before loading the state from the file, a reentrant lock is created, which will except if the file is already locked. Within a lock context manager, we then attempt to recreate the Process from the process state and when successful we acquire the lock.

Parameters:filepath – path to the pickle to be loaded as a Process
Returns:Process instance
get_running_path(pid)[source]

Get the path where the pickle for a process with pid will be stored while it’s running.

Parameters:pid – The process pid
Returns:A string to the absolute path of where the pickle is stored.
Return type:str
load_all_processes()[source]

Will detect all pickles in the running directory and will try to load them up into Processes. As soon as a pickle is considered for loading, a lock is placed on it, which is not released until the process is destroyed. This is necessary to prevent another thread from loading up the same process.

Returns:a list of Process instances
class aiida.work.persistence.RLock(filename, mode='a', timeout=5, check_interval=0.25, fail_when_locked=False, flags=6)[source]

A reentrant lock, functions in a similar way to threading.RLock in that it can be acquired multiple times. When the corresponding number of release() calls are made the lock will finally release the underlying file lock.

class aiida.work.process.Process[source]

This class represents an AiiDA process which can be executed and will have full provenance saved in the database.

class SaveKeys[source]

Keys used to identify things in the saved instance state bundle.

classmethod create_db_record()[source]

Create a database calculation node that represents what happened in this process. :return:

on_destroy()[source]

Called when a Process enters the DESTROYED state which should be the final process state and so we seal the calculation node

on_finish()[source]

Called when a Process enters the FINISHED state at which point we set the corresponding attribute of the workcalculation node

report(msg, *args, **kwargs)[source]

Log a message to the logger, which should get saved to the database through the attached DbLogHandler. The class name and function name of the caller are prepended to the given message

class aiida.work.process_registry.ProcessRegistry[source]

This class is a knowledge provider that uses the AiiDA database to answer questions related to processes.

class aiida.work.run.RunningInfo(type, pid)
pid

Alias for field number 1

type

Alias for field number 0

class aiida.work.run.RunningType[source]

A type to indicate what type of object is running: a process, a calculation or a workflow

aiida.work.run.legacy_calc(pk)[source]

Create a RunningInfo object for a legacy calculation

Parameters:pk (int) – The calculation pk
Returns:The running info
Return type:RunningInfo
aiida.work.run.legacy_workflow(pk)[source]

Create a RunningInfo object for a legacy workflow.

This can be used in conjunction with aiida.work.workchain.ToContext as follows:

>>> from aiida.work.workchain import WorkChain, ToContext, Outputs
>>>
>>> class MyWf(WorkChain):
>>>     @classmethod
>>>     def define(cls, spec):
>>>         super(MyWf, cls).define(spec)
>>>         spec.outline(cls.step1, cls.step2)
>>>
>>>     def step1(self):
>>>         wf = OldEquationOfState()
>>>         wf.start()
>>>         return ToContext(eos=legacy_workflow(wf.pk))
>>>
>>>     def step2(self):
>>>         # Now self.ctx.eos contains the terminated workflow
>>>         pass
Parameters:pk (int) – The workflow pk
Returns:The running info
Return type:RunningInfo
aiida.work.run.queue_up(process_class, inputs, storage)[source]

This queues up the Process so that it’s executed by the daemon when it gets around to it.

Parameters:
  • process_class – The process class to queue up.
  • inputs (Mapping) – The inputs to the process.
  • storage – The storage engine which will be used to save the process (of type plum.persistence)
Returns:

The pid of the queued process.

aiida.work.run.run(process_class, *args, **inputs)[source]

Synchronously (i.e. blocking) run a workfunction or process.

Parameters:
  • process_class – The process class or workfunction
  • _attributes – Optional attributes (only for process)
  • args – Positional arguments for a workfunction
  • inputs – The list of inputs
class aiida.work.test_utils.BadOutput[source]

A Process that emits an output that isn’t part of the spec raising an exception.

class aiida.work.test_utils.DummyProcess[source]

A Process that does nothing when it runs.

class aiida.work.util.ProcessStack[source]

Keep track of the per-thread call stack of processes.

classmethod get_active_process_calc_node()[source]

Get the calculation node of the process at the top of the stack

Returns:The calculation node
Return type:aiida.orm.implementation.general.calculation.job.AbstractJobCalculation
classmethod get_active_process_id()[source]

Get the pid of the process at the top of the stack

Returns:The pid
classmethod pop(process=None, pid=None)[source]

Pop a process from the stack. To make sure the stack is not corrupted the process instance or pid of the calling process should be supplied so we can verify that is really is top of the stack.

Parameters:
  • process – The process instance
  • pid – The process id.
aiida.work.util.get_or_create_output_group(calculation)[source]

For a given Calculation, get or create a new frozendict Data node that has as its values all output Data nodes of the Calculation.

Parameters:calculation – Calculation
aiida.work.util.load_class(classstring)[source]

Load a class from a string

aiida.work.workchain.ToContext(**kwargs)[source]

Utility function that returns a list of UpdateContext Interstep instances

NOTE: This is effectively a copy of WorkChain.to_context method added to keep backwards compatibility, but should eventually be deprecated

class aiida.work.workchain.WorkChain[source]

A WorkChain, the base class for AiiDA workflows.

abort(msg=None, timeout=None)[source]

Abort the workchain by calling the abort method of the Process and also adding the abort message to the report

Parameters:
  • msg (str) – The abort message
  • timeout (float) – Wait for the given time until the process has aborted
Returns:

True if the process is aborted at the end of the function, False otherwise

abort_nowait(msg=None)[source]

Abort the workchain at the next state transition without waiting which is achieved by passing a timeout value of zero

Parameters:msg (str) – The abort message
insert_barrier(wait_on)[source]

Insert a barrier that will cause the workchain to wait until the wait on is finished before continuing to the next step.

Parameters:wait_on – The thing to wait on (of type plum.wait.wait_on)
insert_intersteps(intersteps)[source]

Insert an interstep to be executed after the current step ends but before the next step ends

Parameters:interstep – class:Interstep
remove_barrier(wait_on)[source]

Remove a barrier.

Precondition: must be a barrier that was previously inserted

Parameters:wait_on – The wait on to remove (of type plum.wait.wait_on)
to_context(**kwargs)[source]

This is a convenience method that provides syntactic sugar, for a user to add multiple intersteps that will assign a certain value to the corresponding key in the context of the workchain

aiida.work.workchain.if_(condition)[source]

A conditional that can be used in a workchain outline.

Use as:

if_(cls.conditional)(
  cls.step1,
  cls.step2
)

Each step can, of course, also be any valid workchain step e.g. conditional.

Parameters:condition – The workchain method that will return True or False
aiida.work.workchain.while_(condition)[source]

A while loop that can be used in a workchain outline.

Use as:

while_(cls.conditional)(
  cls.step1,
  cls.step2
)

Each step can, of course, also be any valid workchain step e.g. conditional.

Parameters:condition – The workchain method that will return True or False
class aiida.work.workflow.Workflow[source]

This class represents an AiiDA workflow which can be executed and will have full provenance saved in the database.

This file provides very simple workflows for testing purposes. Do not delete, otherwise ‘verdi developertest’ will stop to work.

aiida.work.workfunction.workfunction(func)[source]

A decorator to turn a standard python function into a workfunction. Example usage:

>>> from aiida.orm.data.base import Int
>>> from aiida.work.workfunction import workfunction as wf
>>>
>>> # Define the workfunction
>>> @wf
>>> def sum(a, b):
>>>    return a + b
>>> # Run it with some input
>>> r = sum(Int(4), Int(5))
>>> print(r)
9
>>> r.get_inputs_dict() 
{u'_return': <WorkCalculation: uuid: ce0c63b3-1c84-4bb8-ba64-7b70a36adf34 (pk: 3567)>}
>>> r.get_inputs_dict()['_return'].get_inputs()
[4, 5]