Contains functions for running tasks in the background.

The associated BackgroundTask object when called in a task. If this is not executed in a background Task None will be returned.

If called from within a task the task id, else a generated uuid List[QueuedTask]

Returns all task which are currently waiting for execution


task_type – Optionally filter by task type


List of queued tasks

Small helper that checks if we are in a task and assuming we are records error_msg as well as setting the completed_successfully to false and completed to True. Note that get_current_task will return None after a call to this.

Small helper that checks if we are in a task and assuming we are records info_msg as info.

Tries to find the related DicomQRRspStudy object given a study instance uid and if this is running in a task will record it to the query object. This is used to later find the import tasks that were run as part of a query. Since this actually just takes the latest query if the user runs imports manually via script it may in principle wrongly associate. str)

Removes task from queue.


task_id – task id of the task in question, task_type, *args, **kwargs) Result

Syntactic sugar around run_in_background_with_limits.

Arguments correspond to run_in_background_with_limits. Will always run the passed function as fast as possible. This function should always be used for functions triggered from the webinterface., task_type, num_proc, num_of_task_type, *args, **kwargs) Result

Runs func as background Process.

This method will create a new task which will be scheduled to be run by the Huey consumer with the specified priority (defaults to 0). The priority can be passed via a keyword argument

Internally the Huey consumer spawns a new process, which then creates a BackgroundTask object. This can be obtained via get_current_task() inside the calling process. Note that BackgroundTask objects will not be deleted onto completion - instead the complete flag will be set to True.

num_proc and num_of_task_type can be used to give conditions on the start.

  • func – The function to run. Note that you should set the status of the task yourself and mark as completed when exiting yourself e.g. via sys.exit(). Assuming the function returns normally on success or returns with an exception on error, the status of the BackgroundTask object will be set correctly.

  • task_type – One of the strings declared in BackgroundTask.task_type. Indicates which kind of background process this is supposed to be. (E.g. move, query, …)

  • num_proc – Will wait with execution until there are less than num_proc active tasks. If num_proc == 0 will run directly

  • num_of_task_type – A dictionary from str to int, where key should be some used task_type. Will wait until there are less active tasks of task_type than specified in the value of the dict for that key.

  • args – Positional arguments. Passed to func.

  • kwargs – Keywords arguments. Passed to func.


The BackgroundTask object. BackgroundTask)

Terminate a background task by force. Sets complete=True on the task object. Result)

Wait until the task has completed