Lockstep-controlled Task.async / Task.await / async_stream style helper.
Example
ctest "concurrent fan-out" do
tasks =
for x <- 1..4 do
Lockstep.Task.async(fn -> x * 2 end)
end
results = Lockstep.Task.await_many(tasks)
assert Enum.sort(results) == [2, 4, 6, 8]
endLimitations
- No
:timeouthonoured -- everything blocks indefinitely under the controller. The runner's per-iteration timeout still applies. async_streamhonours:max_concurrencybut not:on_timeout.Task.Supervisor.async/start_childare routed to bareLockstep.spawn-- no supervisor restart semantics, but task lifecycle is otherwise the same.
Summary
Functions
Spawn a managed task running fun. Returns a task handle to be passed
to await/1 or await_many/1.
3-arg async (Task.async(M, F, A)).
Lockstep version of Task.async_stream/3,4. Honours
:max_concurrency (default System.schedulers_online()); other
options are accepted but ignored. Returns a stream of
{:ok, result} tuples in input order.
4-arg form: async_stream(enum, M, F, A_extra, opts) invokes
apply(M, F, [item | A_extra]) per element.
Block until the task replies with its result. Selective-receive-matched on the task's unique ref so other messages in the caller's mailbox are not disturbed.
Await many tasks; return their results in the order the handles were passed in.
Unlinked task; returns {:ok, pid} to match Task.start/1.
Three-arg variant of start/1.
Spawn a managed linked task; returns {:ok, pid} to match
Task.start_link/1's shape.
Three-arg variant: Task.start_link(M, F, A).
Types
Functions
Spawn a managed task running fun. Returns a task handle to be passed
to await/1 or await_many/1.
3-arg async (Task.async(M, F, A)).
Lockstep version of Task.async_stream/3,4. Honours
:max_concurrency (default System.schedulers_online()); other
options are accepted but ignored. Returns a stream of
{:ok, result} tuples in input order.
Lockstep.Task.async_stream(1..4, fn x -> x * 2 end)
|> Enum.to_list()
# => [{:ok, 2}, {:ok, 4}, {:ok, 6}, {:ok, 8}]
4-arg form: async_stream(enum, M, F, A_extra, opts) invokes
apply(M, F, [item | A_extra]) per element.
Block until the task replies with its result. Selective-receive-matched on the task's unique ref so other messages in the caller's mailbox are not disturbed.
Await many tasks; return their results in the order the handles were passed in.
Unlinked task; returns {:ok, pid} to match Task.start/1.
Three-arg variant of start/1.
Spawn a managed linked task; returns {:ok, pid} to match
Task.start_link/1's shape.
Three-arg variant: Task.start_link(M, F, A).