eqsql.task_queues.core module¶
Task Queue protocol and implementations
- class eqsql.task_queues.core.Future(eq_sql, eq_task_id, tag=None)¶
Bases:
objectRepresents the eventual result of an LocalTaskQueue task. Future instances are returned by the
LocalTaskQueue.submit_task, andLocalTaskQueue.submit_tasksmethods.- Parameters:
eq_sql (TaskQueue) – the LocalTaskQueue instance that created this Future.
eq_task_id (int) – the task id
tag (str) – an optional metadata tag
- cancel()¶
Cancels this Future’s task by removing this Future’s task id from the output queue. Cancelation can fail if this Future task has been popped from the output queue before this call completes. Calling this on an already canceled task will return True.
- Returns:
True if the task is canceled, otherwise False.
- done()¶
Returns True if this Future task has been completed or canceled, otherwise False
- property priority: int¶
Gets the priority of this Future task.
- Returns:
The priority of this Future task.
- result(delay=0.5, timeout=2.0)¶
Gets the result of this future task.
This repeatedly pools the DB for the task result. The polling interval is specified by the delay such that the first interval is defined by the initial delay value which is increased after the first poll. The polling will timeout after the amount of time specified by the timout value is has elapsed.
- Parameters:
delay (float) – the initial polling delay value
timeout (float) – the duration after which the query will timeout. If timeout is None, there is no limit to the wait time.
- Returns:
ResultStatus.SUCCESSorResultStatus.FAILURE, and whose second element is either the result of the task, or in the case of failure the reason for the failure (EQ_TIMEOUT, orEQ_ABORT)- Return type:
A tuple whose first element indicates the status of the query
- property status: TaskStatus¶
Gets the current status of this Future, one of
TaskStatus.QUEUED,TaskStatus.RUNNING,TaskStatus.COMPLETE, orTaskStatus.CANCELED.- Returns:
One of
TaskStatus.QUEUED,TaskStatus.RUNNING,TaskStatus.COMPLETE,TaskStatus.CANCELED, orNoneif the status query fails.
- property worker_pool: str | None¶
Gets the id of the worker pool, if any, that this Future task is running on.
- Returns:
The id of the worker pool that this Future task is running on, or
Noneif the task hasn’t been selected by a worker pool yet.
- class eqsql.task_queues.core.ResultStatus(*values)¶
Bases:
IntEnumEnum defining the status (success or failure) of an EQSQL database operation.
- FAILURE = 1¶
- SUCCESS = 0¶
- exception eqsql.task_queues.core.StopConditionException(msg='StopIterationException', *args, **kwargs)¶
Bases:
Exception
- class eqsql.task_queues.core.TaskQueue(*args, **kwargs)¶
Bases:
ProtocolTask queue protocol for submitting, manipulating and retrieving tasks
- are_queues_empty(eq_type=None)¶
Returns whether or not either of the input or output queues are empty, optionally of a specified task type.
- Parameters:
eq_type (int) – the optional task type to check for.
- Returns:
True if the queues are empty, otherwise False.
- Return type:
bool
- as_completed(futures, pop=False, timeout=None, n=None, batch_size=1, sleep=0)¶
Returns a generator over the
Futuresin thefuturesargument that yields Futures as they complete. TheFuturesare checked for completion by iterating over all of the ones that have not yet completed and checking for a result. At the end of each iteration, thetimeoutis checked. Note that adding or removingFuturesto or from thefuturesargument List while iterating may have no effect on this call. ATimeoutErrorwill be raised if the futures do not complete within the specifiedtimeoutduration.- Parameters:
futures (List[Future]) – the List of
Futuresto iterate over and return as they complete.pop (bool) – if true, completed futures will be popped off of the futures argument List.
timeout (float) – if the time taken for futures to completed is greater than this value, then raise
TimeoutError.n (int) – yield this many completed Futures and then stop iteration.
batch_size (int) – retrieve this many completed futures, before yielding.
sleep (float) – the time, in seconds, to sleep between each iteration over all the Futures.
- Return type:
Generator[Future, None, None]
Yields:
Futuresin thefuturesargument as they complete.Examples
>>> futures = [] // submit some tasks and append the Future instances to futures for ft in task_queue.as_completed(futures, timeout=5): status, result = ft.result() // do something with result
- cancel_tasks(futures)¶
Cancels the specified
Futures.- Parameters:
- Returns:
A tuple containing the
ResultStatusand the ids of the successfully canceled tasks.- Return type:
Tuple[ResultStatus, int]
- get_priorities(futures)¶
Gets the priorities of the specified tasks.
- get_status(futures)¶
Gets the status (queued, running, etc.) of the specified tasks
- Parameters:
futures (Iterable[Future]) – the futures of the tasks to get the status of.
- Returns:
A List of Tuples containing the status of the tasks. The first element of the tuple will be the task id, and the second element will be the status of that task as a
TaskStatusobject.- Return type:
List[Tuple[Future, TaskStatus]]
- get_worker_pools(futures)¶
Gets the worker pools on which the specified list of
Futuresare running, if any. All the specifiedFuturesmust have been submitted by the same task queue (i.e., they are all in the same eqsql database).
- pop_completed(futures, timeout=None, sleep=0)¶
Pops and returns the first completed future from the specified List of Futures.
- Parameters:
futures (List[Future]) – the List of
Futuresto check for a completed one. The completedFuturewill be popped from this list.timeout – a
TimeoutErrorwill be raised if a completed Future cannot be returned by after this amount time.sleep (float) – the time, in seconds, to sleep between each iteration over all the Futures, when looking for a completed one.
- Returns:
The first completed
Futurefrom the specified List ofFutures.- Return type:
- query_result(eq_task_id, delay=0.5, timeout=2.0)¶
Queries for the result of the specified task.
The query repeatedly polls for a result. The polling interval is specified by the delay such that the first interval is defined by the initial delay value which is increased after the first poll. The polling will timeout after the amount of time specified by the timout value is has elapsed.
- Parameters:
eq_task_id (int) – the id of the task to query
delay (float) – the initial polling delay value
timeout (float) – the duration after which the query will timeout. If timeout is None, there is no limit to the wait time.
- Returns:
ResultStatus.SUCCESSorResultStatus.FAILURE, and whose second element is either the result of the task, or in the case of failure the reason for the failure (EQ_TIMEOUT, orEQ_ABORT)- Return type:
A tuple whose first element indicates the status of the query
- submit_task(exp_id, eq_type, payload, priority=0, tag=None)¶
Submits work of the specified type and priority with the specified payload, returning the
statusand theFutureencapsulating the submission.- Parameters:
exp_id (str) – the id of the experiment of which the work is part.
eq_type (int) – the type of work
payload (str) – the work payload
priority (int) – the priority of this work
tag (str) – an optional metadata tag for the task
- Returns:
A tuple containing the status (
ResultStatus.FAILUREorResultStatus.SUCCESS) of the submission and if successful, aFuturerepresenting the submitted task otherwise None.- Return type:
Tuple[ResultStatus, Future | None]
- submit_tasks(exp_id, eq_type, payload, priority=0, tag=None)¶
Submits work of the specified type and priority with the specified payloads, returning the
statusand thefuturesencapsulating the submission.- Parameters:
exp_id (str) – the id of the experiment of which the work is part.
eq_type (int) – the type of work
payload (List[str]) – a list of the work payloads
priority (int) – the priority of this work
tag (str) – an optional metadata tag for the tasks
- Returns:
A tuple containing the status (
ResultStatus.FAILUREorResultStatus.SUCCESS) of the submission and the list offuturesfor the submitted tasks. If the submission fails, the list offutureswill contain thefuturesthat submitted sucessfully.- Return type:
Tuple[ResultStatus, List[Future]]
- update_priorities(futures, new_priority)¶
Updates the priority of the specified
Futuresto the new_priority.- Parameters:
new_priority (int | List[int]) – the priority to update to. If this is a single integer then all the specified tasks are updated to that priority. If this is a List of ints then each task is updated with the corresponding priority, i.e., the first task in the
futuresis updated with the first priority in the new_priority List.
- Returns:
- If the update is successful, the Tuple will contain ResultStatus.SUCCESS and
the eq_task_ids of the tasks whose priority was successfully updated, otherwise (ResultStatus.FAILURE, []).
- Return type:
Tuple[ResultStatus, int]
- class eqsql.task_queues.core.TaskStatus(*values)¶
Bases:
IntEnumEnum defining the status of a task: queued, etc. These are used in the database to store the status of a task.
- CANCELED = 3¶
- COMPLETE = 2¶
- QUEUED = 0¶
- REQUEUED = 4¶
- RUNNING = 1¶
- exception eqsql.task_queues.core.TimeoutError(msg='TimeoutError', *args, **kwargs)¶
Bases:
ExceptionException used to indicate that a query has timed out.