Tasks

Core.Task — Type.

  1. Task(func)

Create a Task (i.e. coroutine) to execute the given function func (which must be callable with no arguments). The task exits when this function returns.

Examples

  1. julia> a() = sum(i for i in 1:1000);
  2. julia> b = Task(a);

In this example, b is a runnable Task that hasn't started yet.

source

Base.current_task — Function.

  1. current_task()

Get the currently running Task.

source

Base.istaskdone — Function.

  1. istaskdone(t::Task) -> Bool

Determine whether a task has exited.

Examples

  1. julia> a2() = sum(i for i in 1:1000);
  2. julia> b = Task(a2);
  3. julia> istaskdone(b)
  4. false
  5. julia> schedule(b);
  6. julia> yield();
  7. julia> istaskdone(b)
  8. true

source

Base.istaskstarted — Function.

  1. istaskstarted(t::Task) -> Bool

Determine whether a task has started executing.

Examples

  1. julia> a3() = sum(i for i in 1:1000);
  2. julia> b = Task(a3);
  3. julia> istaskstarted(b)
  4. false

source

Base.yield — Function.

  1. yield()

Switch to the scheduler to allow another scheduled task to run. A task that calls this function is still runnable, and will be restarted immediately if there are no other runnable tasks.

source

  1. yield(t::Task, arg = nothing)

A fast, unfair-scheduling version of schedule(t, arg); yield() which immediately yields to t before calling the scheduler.

source

Base.yieldto — Function.

  1. yieldto(t::Task, arg = nothing)

Switch to the given task. The first time a task is switched to, the task's function is called with no arguments. On subsequent switches, arg is returned from the task's last call to yieldto. This is a low-level call that only switches tasks, not considering states or scheduling in any way. Its use is discouraged.

source

Base.task_local_storage — Method.

  1. task_local_storage(key)

Look up the value of a key in the current task's task-local storage.

source

Base.task_local_storage — Method.

  1. task_local_storage(key, value)

Assign a value to a key in the current task's task-local storage.

source

Base.task_local_storage — Method.

  1. task_local_storage(body, key, value)

Call the function body with a modified task-local storage, in which value is assigned to key; the previous value of key, or lack thereof, is restored afterwards. Useful for emulating dynamic scoping.

source

Base.Condition — Type.

  1. Condition()

Create an edge-triggered event source that tasks can wait for. Tasks that call wait on a Condition are suspended and queued. Tasks are woken up when notify is later called on the Condition. Edge triggering means that only tasks waiting at the time notify is called can be woken up. For level-triggered notifications, you must keep extra state to keep track of whether a notification has happened. The Channel type does this, and so can be used for level-triggered events.

source

Base.notify — Function.

  1. notify(condition, val=nothing; all=true, error=false)

Wake up tasks waiting for a condition, passing them val. If all is true (the default), all waiting tasks are woken, otherwise only one is. If error is true, the passed value is raised as an exception in the woken tasks.

Return the count of tasks woken up. Return 0 if no tasks are waiting on condition.

source

Base.schedule — Function.

  1. schedule(t::Task, [val]; error=false)

Add a Task to the scheduler's queue. This causes the task to run constantly when the system is otherwise idle, unless the task performs a blocking operation such as wait.

If a second argument val is provided, it will be passed to the task (via the return value of yieldto) when it runs again. If error is true, the value is raised as an exception in the woken task.

Examples

  1. julia> a5() = sum(i for i in 1:1000);
  2. julia> b = Task(a5);
  3. julia> istaskstarted(b)
  4. false
  5. julia> schedule(b);
  6. julia> yield();
  7. julia> istaskstarted(b)
  8. true
  9. julia> istaskdone(b)
  10. true

source

Base.@task — Macro.

  1. @task

Wrap an expression in a Task without executing it, and return the Task. This only creates a task, and does not run it.

Examples

  1. julia> a1() = sum(i for i in 1:1000);
  2. julia> b = @task a1();
  3. julia> istaskstarted(b)
  4. false
  5. julia> schedule(b);
  6. julia> yield();
  7. julia> istaskdone(b)
  8. true

source

Base.sleep — Function.

  1. sleep(seconds)

Block the current task for a specified number of seconds. The minimum sleep time is 1 millisecond or input of 0.001.

source

Base.Channel — Type.

  1. Channel{T}(sz::Int)

Constructs a Channel with an internal buffer that can hold a maximum of sz objects of type T. put! calls on a full channel block until an object is removed with take!.

Channel(0) constructs an unbuffered channel. put! blocks until a matching take! is called. And vice-versa.

Other constructors:

  • Channel(Inf): equivalent to Channel{Any}(typemax(Int))
  • Channel(sz): equivalent to Channel{Any}(sz)

source

Base.put! — Method.

  1. put!(c::Channel, v)

Append an item v to the channel c. Blocks if the channel is full.

For unbuffered channels, blocks until a take! is performed by a different task.

source

Base.take! — Method.

  1. take!(c::Channel)

Remove and return a value from a Channel. Blocks until data is available.

For unbuffered channels, blocks until a put! is performed by a different task.

source

Base.isready — Method.

  1. isready(c::Channel)

Determine whether a Channel has a value stored to it. Returns immediately, does not block.

For unbuffered channels returns true if there are tasks waiting on a put!.

source

Base.fetch — Method.

  1. fetch(c::Channel)

Wait for and get the first available item from the channel. Does not remove the item. fetch is unsupported on an unbuffered (0-size) channel.

source

Base.close — Method.

  1. close(c::Channel)

Close a channel. An exception is thrown by:

  • put! on a closed channel.
  • take! and fetch on an empty, closed channel.

source

Base.bind — Method.

  1. bind(chnl::Channel, task::Task)

Associate the lifetime of chnl with a task. Channel chnl is automatically closed when the task terminates. Any uncaught exception in the task is propagated to all waiters on chnl.

The chnl object can be explicitly closed independent of task termination. Terminating tasks have no effect on already closed Channel objects.

When a channel is bound to multiple tasks, the first task to terminate will close the channel. When multiple channels are bound to the same task, termination of the task will close all of the bound channels.

Examples

  1. julia> c = Channel(0);
  2. julia> task = @async foreach(i->put!(c, i), 1:4);
  3. julia> bind(c,task);
  4. julia> for i in c
  5. @show i
  6. end;
  7. i = 1
  8. i = 2
  9. i = 3
  10. i = 4
  11. julia> isopen(c)
  12. false
  1. julia> c = Channel(0);
  2. julia> task = @async (put!(c,1);error("foo"));
  3. julia> bind(c,task);
  4. julia> take!(c)
  5. 1
  6. julia> put!(c,1);
  7. ERROR: foo
  8. Stacktrace:
  9. [...]

source

Base.asyncmap — Function.

  1. asyncmap(f, c...; ntasks=0, batch_size=nothing)

Uses multiple concurrent tasks to map f over a collection (or multiple equal length collections). For multiple collection arguments, f is applied elementwise.

ntasks specifies the number of tasks to run concurrently. Depending on the length of the collections, if ntasks is unspecified, up to 100 tasks will be used for concurrent mapping.

ntasks can also be specified as a zero-arg function. In this case, the number of tasks to run in parallel is checked before processing every element and a new task started if the value of ntasks_func is less than the current number of tasks.

If batch_size is specified, the collection is processed in batch mode. f must then be a function that must accept a Vector of argument tuples and must return a vector of results. The input vector will have a length of batch_size or less.

The following examples highlight execution in different tasks by returning the objectid of the tasks in which the mapping function is executed.

First, with ntasks undefined, each element is processed in a different task.

  1. julia> tskoid() = objectid(current_task());
  2. julia> asyncmap(x->tskoid(), 1:5)
  3. 5-element Array{UInt64,1}:
  4. 0x6e15e66c75c75853
  5. 0x440f8819a1baa682
  6. 0x9fb3eeadd0c83985
  7. 0xebd3e35fe90d4050
  8. 0x29efc93edce2b961
  9. julia> length(unique(asyncmap(x->tskoid(), 1:5)))
  10. 5

With ntasks=2 all elements are processed in 2 tasks.

  1. julia> asyncmap(x->tskoid(), 1:5; ntasks=2)
  2. 5-element Array{UInt64,1}:
  3. 0x027ab1680df7ae94
  4. 0xa23d2f80cd7cf157
  5. 0x027ab1680df7ae94
  6. 0xa23d2f80cd7cf157
  7. 0x027ab1680df7ae94
  8. julia> length(unique(asyncmap(x->tskoid(), 1:5; ntasks=2)))
  9. 2

With batch_size defined, the mapping function needs to be changed to accept an array of argument tuples and return an array of results. map is used in the modified mapping function to achieve this.

  1. julia> batch_func(input) = map(x->string("args_tuple: ", x, ", element_val: ", x[1], ", task: ", tskoid()), input)
  2. batch_func (generic function with 1 method)
  3. julia> asyncmap(batch_func, 1:5; ntasks=2, batch_size=2)
  4. 5-element Array{String,1}:
  5. "args_tuple: (1,), element_val: 1, task: 9118321258196414413"
  6. "args_tuple: (2,), element_val: 2, task: 4904288162898683522"
  7. "args_tuple: (3,), element_val: 3, task: 9118321258196414413"
  8. "args_tuple: (4,), element_val: 4, task: 4904288162898683522"
  9. "args_tuple: (5,), element_val: 5, task: 9118321258196414413"

Note

Currently, all tasks in Julia are executed in a single OS thread co-operatively. Consequently, asyncmap is beneficial only when the mapping function involves any I/O - disk, network, remote worker invocation, etc.

source

Base.asyncmap! — Function.

  1. asyncmap!(f, results, c...; ntasks=0, batch_size=nothing)

Like asyncmap, but stores output in results rather than returning a collection.

source