Skip to main content
All Python modules · Import and compatibility guidance Use task to decorate application work. It returns a TaskCallable whose direct call runs inline and whose defer method submits queued work. The returned TaskHandle exposes status, cancellation, and result access. For discovery, storage setup, and durable waiting, see Create and run Tasks, Task and cron storage, and Durable execution.

Task parameters

The names idempotency_key and schedule_in are options on .defer(); avoid using them as task input names. Arguments and results must be strict JSON values: no credentials, arbitrary Python objects, non-string object keys, or non-finite numbers. Workers may retry, so task side effects must be idempotent.

Handle behavior

Handles belong to the compiled runtime that returned them. There is no public handle reconstruction or job-listing API. Calling .defer() without an active compiled runtime raises TaskUnavailableError; calling the decorated function directly does not require queue storage. TaskStore and TaskRecord are storage adapter contracts. TaskDefinition and CompiledTask describe compiler/runtime registrations. Use Task and cron storage to choose a production provider; MemoryTaskStore does not make work survive a process restart.

Storage protocols

Exports

CompiledTask

One compiler-named task executable in the compiled application runtime.
Fields (— means no declared default):

CompiledTask.function

Return the original callable only at the trusted worker boundary.

CompiledTask.queue

Return the authored queue without exposing the definition object.

CompiledTask.max_retries

Return the bounded retry count used during native registration.

MemoryTaskStore

Implement atomic contracts in one process; records do not survive restart.

MemoryTaskStore.start

Satisfy lifecycle ownership without opening external resources.

MemoryTaskStore.close

Release no resources; in-memory data lives only with this instance.

MemoryTaskStore.enqueue_task

Commit an isolated task snapshot and its deduplication identity.

MemoryTaskStore.get_task

Read one detached snapshot through the caller’s ownership scope.

MemoryTaskStore.list_task_metadata

Return a bounded owner-scoped metadata page without copying private payloads.

MemoryTaskStore.claim_tasks

Claim due work, reclaim expired leases and retire exhausted attempts.

MemoryTaskStore.renew_task_lease

Extend only a lease that still grants execution ownership.

MemoryTaskStore.finish_task

Commit an outcome only while the exact execution lease remains valid.

MemoryTaskStore.cancel_task

Cancel one owner’s active work and revoke any in-flight attempt.

MemoryTaskStore.create_cron

Create a schedule or return an exact keyed retry without reactivation.

MemoryTaskStore.get_cron

Read a detached schedule through both ownership predicates.

MemoryTaskStore.list_crons

Return one bounded page from this in-memory reference collection.

MemoryTaskStore.list_cron_metadata

Project an owner-scoped schedule page without copying task arguments.

MemoryTaskStore.update_cron

Replace allowed fields only at the current scoped revision.

MemoryTaskStore.delete_cron

Remove one owner’s schedule and its reusable authored key.

MemoryTaskStore.list_due_crons

Read active due schedules with a stable timestamp-and-ID cursor.

MemoryTaskStore.commit_cron_occurrence

Use one lock for revision check, task insert and schedule advancement.

PostgresTaskStore

Persist jobs and schedules with transactional claims and fenced leases. _pool supports composing this provider with the combined session store; externally supplied pools remain owned by their caller.

PostgresTaskStore.start

Open one pool and install or validate the durable schema.

PostgresTaskStore.close

Close only the pool opened by this instance.

PostgresTaskStore.create_cron

Create a schedule once without reactivating an existing cancelled key.

PostgresTaskStore.get_cron

Read a schedule only within its application and owner scope.

PostgresTaskStore.list_crons

Apply owner filtering and keyset pagination in the database.

PostgresTaskStore.update_cron

Replace one schedule conditionally, preserving its immutable identity.

PostgresTaskStore.delete_cron

Delete a schedule using the same owner predicate as reads.

PostgresTaskStore.list_due_crons

Page due schedules in database order without loading inactive rows.

PostgresTaskStore.commit_cron_occurrence

Atomically enqueue an occurrence and advance its schedule revision.

PostgresTaskStore.enqueue_task

Persist an immutable task identity, returning exact idempotent retries.

PostgresTaskStore.get_task

Read a scoped job; omitted user scope is reserved for trusted workers.

PostgresTaskStore.claim_tasks

Claim disjoint batches and retire exhausted attempts after crashes.

PostgresTaskStore.renew_task_lease

Renew only an unexpired attempt still owned by the calling worker.

PostgresTaskStore.finish_task

Fence completion and retries; terminal transitions erase private input.

PostgresTaskStore.cancel_task

Cancel an owned nonterminal job and revoke any running attempt.

PostgresTaskStore.list_task_metadata

Push owner, cursor, projection, ordering, and limit into one database query.

PostgresTaskStore.list_cron_metadata

Read a bounded metadata projection with owner filtering in one SQL query.

TaskCallable

Preserve direct invocation while adding an asynchronous defer boundary.

TaskCallable.call

Execute inline when the author deliberately calls the task itself.

TaskCallable.defer

Commit safe arguments to the active compiled task runtime.

TaskDefinition

Authoring metadata retained on a decorated function.
Fields (— means no declared default):

TaskHandle

Opaque queue handle returned after a job is committed. Obtain this object from the owning Harnest API; its constructor includes runtime bindings. Fields (— means no declared default):

TaskHandle.status

Read the native job state through the owning runtime.

TaskHandle.cancel

Cancel queued work without exposing the backend application.

TaskHandle.result

Return completed work or suspend through a durable managed tool.

TaskRecord

Private persisted job, including reconstructable invocation capabilities. Epoch timestamps are UTC seconds. Attempts count claims, starting at one; max_retries permits that many attempts after the first. Providers retain an immutable enqueue fingerprint after clearing terminal private payloads.
Fields (— means no declared default):

TaskStore

Atomic operations required for at-least-once task execution. All selection, scope predicates, ordering, and limits execute inside the datastore. A provider must durably commit before returning success. Expired lease holders cannot renew or finish work. Terminal transitions clear arguments, invocation and permissions while retaining the result/failure.

TaskStore.start

Open owned resources and initialize the provider’s durable schema.

TaskStore.close

Release owned resources after workers and dispatchers have stopped.

TaskStore.enqueue_task

Atomically persist payload and job, or return an identical submission. Idempotency is scoped by application, user, task name and optional key. Conflicting definitions raise TaskStoreConflictError. Persist a separate fingerprint so terminal payload cleanup cannot defeat deduplication.

TaskStore.get_task

Read one scoped job; omitted user is a trusted worker-only lookup.

TaskStore.claim_tasks

Atomically claim due pending or expired running jobs in queue scope. Return a bounded eligible batch with best-effort earliest-due ordering; concurrent workers and lease recovery do not guarantee strict FIFO. Each claim increments attempt and issues a fresh lease token. Expired final attempts become failed with task_failed instead of remaining stranded or retrying indefinitely. Empty queues selects no work. Limits are integers from 1 to 1000 and apply to returned claims.

TaskStore.renew_task_lease

Extend a running, unexpired matching lease; stale owners return false.

TaskStore.finish_task

Fence a completion, failure or retry with a matching unexpired lease. Status is completed, failed, or pending. Pending requires retry_at and remaining retries; it preserves payload. All transitions clear leases. Stale owners return false and must never overwrite another attempt.

TaskStore.cancel_task

Atomically cancel pending/running work, invalidate leases and scrub. Unknown, foreign and terminal jobs return false. A running effect may already have happened; cancellation prevents subsequent state commits.

TaskStoreConflictError

A stable job identity or idempotency key has a different definition.

TaskUnavailableError

A task was deferred without its compiled runtime being active.

safe_task_result

Normalize a task result to the strict JSON values safe for persistence.

task

Mark a callable for queue, scheduling, and retry execution. Deferred execution uses Harnest workers and the application’s configured lifecycle.storage.tasks provider. Durable storage makes queued work recoverable across restarts; importing this API requires no queue library. Direct calls remain ordinary Python calls; defer is queue execution and never promises restoration of a suspended Python frame.