Title here
Summary here
Julia’s Channels and tasks allow you to wait on multiple operations simultaneously. Combining tasks and channels with select is a powerful feature of Julia.
using Dates
function main()
# For our example we'll select across two channels.
c1 = Channel{String}(1)
c2 = Channel{String}(1)
# Each channel will receive a value after some amount
# of time, to simulate e.g. blocking RPC operations
# executing in concurrent tasks.
@async begin
sleep(1)
put!(c1, "one")
end
@async begin
sleep(2)
put!(c2, "two")
end
# We'll use `@select` to await both of these values
# simultaneously, printing each one as it arrives.
for i in 1:2
@select begin
msg1 = take!(c1) => println("received ", msg1)
msg2 = take!(c2) => println("received ", msg2)
end
end
end
main()We receive the values "one" and then "two" as expected.
$ julia select.jl
received one
received twoNote that the total execution time is only ~2 seconds since both the 1 and 2 second sleeps execute concurrently.
real 0m2.245sIn this Julia version:
Channel{String}(1) to create buffered channels that can hold one string each.@async macro to create tasks that run concurrently.select statement in Go is replaced by Julia’s @select macro, which has a similar functionality.put! to send values to channels and take! to receive from them.fmt.Println function is replaced with Julia’s println.The overall structure and behavior of the program remain the same, demonstrating how to wait on multiple channel operations simultaneously in Julia.