Select.jl

Author NHDaly
Popularity
6 Stars
Updated Last
1 Year Ago
Started In
September 2019

Select

This repo is branched from https://github.com/durcan/Select.jl, which was originally a copy of Jon Malmaud's go-inspired select macro for the Julia programming language. I have updated the repo for Julia 1.3+, multithreaded the Select macro, and hardened the code a bit.

Install this package via:

julia> Pkg.add("https://github.com/NHDaly/Select.jl")

The original README from durcan/Select.jl follows:


This is copy of Jon Malmaud's go inspired select macro for the Julia programming language. I have made a slight syntax modification, but essentially all the code is his.

A select expression is for waiting on multiple communication operations and is of the form:

@select begin
     clause1 => body1
     clause2 => body2
     _       => default_body
    end
end

Wait for multiple clauses simultaneously using an pattern matching syntax, taking a different action depending on which clause is available first. A clause has three possible forms:

  1. event |> value If event is an AbstractChannel, wait for a value to become available in the channel and assign take!(event) to value. if event is a Task, wait for the task to complete and assign value the return value of the task.
  2. event |< value Only suppored for AbstractChannels. Wait for the channel to capabity to store an element, and then call put!(event, value).
  3. event Calls wait on event, discarding the return value. Usable on any "waitable" events", which include channels, tasks, Condition objects, and processes.

If a default branch is provided, @select will check arbitrary choose any event which is ready and execute its body, or will execute default_body if none of them are.

Otherise, @select blocks until at least one event is ready.

For example,

channel1 = Channel()
channel2 = Channel()
task = @task ...
result = @select begin
    channel1 |> value => begin
            info("Took from channel1")
            value
        end
    channel2 <| :test => info("Put :test into channel2")
    task              => info("task finished")
end