slurmer package

Slurmer package.

Use this package to run tasks in a computing cluster. Supported task schedulers are:
class Task(cluster_id=None, cluster_total=None)[source]

Bases: abc.ABC

Base class defining a set of Tasks to be done.

Initialize the class.

Parameters
  • cluster_id (Optional[int]) – Id of the current node in the cluster, if None the value is read from SLURM environment variable SLURM_ARRAY_TASK_ID. Defaults to None.

  • cluster_total (Optional[int], optional) – Number of allocated nodes in the cluster, if None the value is read from the SLURM environment variable SLURM_ARRAY_TASK_MAX. Defaults to None.

abstract generate_parameters()[source]

Generate all the parameters for the different tasks to run.

This function must be implemented. It should return an iterator over all the parameters that should be passed to the processor_function(). The number of tasks to run is determined by the number of parameters returned by this method.

Return type

Iterator[TaskParameters]

make_dirs()[source]

Make directories before execution.

If some directories need to be created before executing the tasks, inherit this method and create the directories here. Override this function to add some behaviour.

abstract static processor_function(parameters)[source]

Execute the task.

This function must be implemented. Here you can run the task that you want to run. It should return a TaskResult if the execution worked as expected (even if it’s just an empty object), None if the execution failed and execution should be terminated.

Return type

Optional[TaskResult]

process_output(result)[source]

Process the generated output.

Process the output generated with processor_function() and evaluate whether execution should be terminated. Override this method if the default behaviour (return False) should be changed.

Parameters

result (TaskResult) – Result of the task as returned by the processor_function()

Returns

True if the execution should be terminated, False otherwise.

Return type

bool

after_run()[source]

Handle results after running tasks.

Override this method to handle results after everything has been run. Keep in mind that this is run after all the tasks of this fold have been run but that in another node they may still be running.

key_interrupt()[source]

Override this method to handle the status after a keyboard interrupt.

static get_cluster_ids(cluster_id, cluster_total)[source]

Get the cluster id and the total number of nodes.

If the cluster_id and cluster_total are not set, they are obtained from the environment variables passed by the SLURM task scheduler.

Parameters
  • cluster_id (Optional[int]) – ID of the current node.

  • cluster_total (Optional[int]) – Total number of nodes nodes that we can use in the cluster.

Returns

Tuple with current cluster ID and total number of nodes.

Return type

tuple[int, int]

execute_tasks(make_dirs_only=False, debug=False, processes=None, no_bar=False, description='')[source]

Execute all the tasks.

The tasks are executed in a random order so do not rely on the order and use IDs instead. Even if the order is random, it is consistent across nodes.

Parameters
  • make_dirs_only (bool, optional) – If True, only the directories will be created and execution will return. Defaults to False.

  • debug (bool, optional) – If True, the execution will be run in debug mode disabling all the parallelism. Defaults to False.

  • processes (int, optional) – Number of processes to use. Pass None to use as many as the system has. Defaults to None.

  • no_bar (bool, optional) – If True, the progress bar will not be displayed. Defaults to False.

  • description (str, optional) – Description of the task. Defaults to “”.

Raises
  • KeyboardInterrupt – If a keyboard interrupt is passed, all the tasks are stopped and a KeyboardInterrupt is raised.

  • TaskFailedError – If one of the tasks returns a strange result then a TaskFailedError is raised.

Returns

The termination result of the tasks. If everything is fine, the result is Error.None.

Return type

Error

exception TaskFailedError[source]

Bases: Exception

Exception thrown if one of the tasks fails.

args
with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

class TaskParameters[source]

Bases: object

Data class containing the parameters that should be used by a task.

Inherit this class to add parameters to your task.

class TaskResult[source]

Bases: object

Data class containing the result of a task.

Inherit this class to add results to your task.

Example

You can inherit this class as follows:

1from dataclass import dataclass
2from slurmer import TaskResult
3
4@dataclass
5class MyTaskResult(TaskResult):
6    my_result: int

Submodules

slurmer.task_runner module

class Error(value)[source]

Bases: enum.Enum

Type of termination error.

NONE = 1

No error occurred during execution

TERMINATE = 2

A keyboard interrupt was received

SYSTEM = 3

A task produced and undesired output

class TaskParameters[source]

Bases: object

Data class containing the parameters that should be used by a task.

Inherit this class to add parameters to your task.

class TaskResult[source]

Bases: object

Data class containing the result of a task.

Inherit this class to add results to your task.

Example

You can inherit this class as follows:

1from dataclass import dataclass
2from slurmer import TaskResult
3
4@dataclass
5class MyTaskResult(TaskResult):
6    my_result: int
exception TaskFailedError[source]

Bases: Exception

Exception thrown if one of the tasks fails.

args
with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

class Task(cluster_id=None, cluster_total=None)[source]

Bases: abc.ABC

Base class defining a set of Tasks to be done.

Initialize the class.

Parameters
  • cluster_id (Optional[int]) – Id of the current node in the cluster, if None the value is read from SLURM environment variable SLURM_ARRAY_TASK_ID. Defaults to None.

  • cluster_total (Optional[int], optional) – Number of allocated nodes in the cluster, if None the value is read from the SLURM environment variable SLURM_ARRAY_TASK_MAX. Defaults to None.

abstract generate_parameters()[source]

Generate all the parameters for the different tasks to run.

This function must be implemented. It should return an iterator over all the parameters that should be passed to the processor_function(). The number of tasks to run is determined by the number of parameters returned by this method.

Return type

Iterator[TaskParameters]

make_dirs()[source]

Make directories before execution.

If some directories need to be created before executing the tasks, inherit this method and create the directories here. Override this function to add some behaviour.

abstract static processor_function(parameters)[source]

Execute the task.

This function must be implemented. Here you can run the task that you want to run. It should return a TaskResult if the execution worked as expected (even if it’s just an empty object), None if the execution failed and execution should be terminated.

Return type

Optional[TaskResult]

process_output(result)[source]

Process the generated output.

Process the output generated with processor_function() and evaluate whether execution should be terminated. Override this method if the default behaviour (return False) should be changed.

Parameters

result (TaskResult) – Result of the task as returned by the processor_function()

Returns

True if the execution should be terminated, False otherwise.

Return type

bool

after_run()[source]

Handle results after running tasks.

Override this method to handle results after everything has been run. Keep in mind that this is run after all the tasks of this fold have been run but that in another node they may still be running.

key_interrupt()[source]

Override this method to handle the status after a keyboard interrupt.

static get_cluster_ids(cluster_id, cluster_total)[source]

Get the cluster id and the total number of nodes.

If the cluster_id and cluster_total are not set, they are obtained from the environment variables passed by the SLURM task scheduler.

Parameters
  • cluster_id (Optional[int]) – ID of the current node.

  • cluster_total (Optional[int]) – Total number of nodes nodes that we can use in the cluster.

Returns

Tuple with current cluster ID and total number of nodes.

Return type

tuple[int, int]

execute_tasks(make_dirs_only=False, debug=False, processes=None, no_bar=False, description='')[source]

Execute all the tasks.

The tasks are executed in a random order so do not rely on the order and use IDs instead. Even if the order is random, it is consistent across nodes.

Parameters
  • make_dirs_only (bool, optional) – If True, only the directories will be created and execution will return. Defaults to False.

  • debug (bool, optional) – If True, the execution will be run in debug mode disabling all the parallelism. Defaults to False.

  • processes (int, optional) – Number of processes to use. Pass None to use as many as the system has. Defaults to None.

  • no_bar (bool, optional) – If True, the progress bar will not be displayed. Defaults to False.

  • description (str, optional) – Description of the task. Defaults to “”.

Raises
  • KeyboardInterrupt – If a keyboard interrupt is passed, all the tasks are stopped and a KeyboardInterrupt is raised.

  • TaskFailedError – If one of the tasks returns a strange result then a TaskFailedError is raised.

Returns

The termination result of the tasks. If everything is fine, the result is Error.None.

Return type

Error