task.core
This namespace contains the core components of the task libary.
For task creation, see run.
For task combinators, see then, compose, for and sequence.
To use custom executors, you have either two options: implicitly via dynamic binding, or explicitly using an additional parameter.
By default, the executor is bound to the variable *pool*. This can be re-bound dynamically like this:
(binding [*pool* (Executors/newFixedThreadPool 4)]
@(then inc (task/run 1234)))
By default, *pool*
is bound to common-pool.
Otherwise, you can use the parameter versions. The explicit versions have -in
appended to their names. * run and run-in * then and then-in * compose and compose-in * for and for-in * sequence and sequence-in
For more information see the documentation about the execution model.
*pool*
dynamic
The dynamic binding for the executor. Re-bind this var to set a new executor for that scope. Defaults to ForkJoinPool.
compose
(compose func)
(compose func task)
(compose func task & more)
Chain two tasks together. Applies func
, which is a function returning a task, to the result of task
. This returns a new task.
Should be used when the function to then returns a task, which would otherwise result in a task inside a task.
With one argument, create a function that accepts a task. With two args, apply func
to task
directly. With more than two args, return a task that completes with a list of func
composed with the results.
By default, it uses the executor bound to *pool*.
See also compose-in.
compose-in
(compose-in executor f)
(compose-in executor f task)
(compose-in executor f task & more)
Like compose, but use the explicit executor given in executor
.
for
macro
(for bindings & body)
Chain multiple tasks together. Returns a new task that evaluates when all the tasks are ready together. The task evaluates to body
. Uses the executor bound to *pool* by default.
for-in
macro
(for-in executor bindings & body)
Chain multiple tasks together. Returns a new task that evaluates when all the tasks are ready together. The task evaluates to body
. Uses the executor
executor.
run
macro
(run & body)
Create a task. Runs body
according to the execution model. Produces a Task that evaluates to the result of body
.
This may mean running in another thread. The default behaviour is the executor bound to *pool* which body
inside the ForkJoinPool executor.
sequence
(sequence tasks)
Turn a sequence of tasks into a task of a sequence. Returns a new task returning a vector of all the results of each task. The task evaluates when all the tasks evaluate.
Rebind *pool* to change the executor.
Task
protocol
members
cancel
(cancel this)
Attempt to cancel the future, returns true if it cancelled.
cancelled?
(cancelled? this)
Was the task cancelled?
complete!
(complete! this)
(complete! this value)
Complete the task with some value if it hasn’t completed already.
done?
(done? this)
Has the task completed in any fashion?
else
(else this value)
Get the value of the task if it’s complete. Otherwise return value
. This is non-blocking.
fail!
(fail! this throwable)
Force the task to fail with throwable
whether or not already completed.
failed?
(failed? this)
Did the task complete with an exception of any kind?
failure
(failure this)
If the task has completed exceptionally, get the Throwable that produced the exceptional completion.
force!
(force! this value)
Force the task to return value
whether or not already completed.
recover
(recover this f)
Recover possible failures in task
. Returns a new task. This task evaluates to two possible values. If the task completes with an exception, this exception is passed to func
, and the task evaluates to its result. If the task completes normally, the task will evaluate to that result.
task->future
(task->future this)
Convert this task into a CompletableFuture.
then
(then func)
(then func task)
(then func task & more)
Apply a function to the result of task
. Returns a new task.
With one argument, creates a function that accepts a task, and applies func
on that task.
With two arguments, applies func
to task
.
With more than two arguments, returns a task of a vector with all the results of func
applied to the arguments.
By default, it uses the executor bound to *pool*.
See then-in.
then-in
(then-in executor f)
(then-in executor f task)
(then-in executor f task & more)
Like then, but use the explicit executor given in executor
.
traverse
(traverse xs f)
Map over a sequence with a function that produces a task, and capture the result of the whole operation in a task. Given f
which is a function from something to a task, traverse
produces a task that evaluates into a list as if f were map
d over it.