June 2019
Intermediate to advanced
218 pages
5h 19m
English
A crucial feature of Channels is that they are iterators. In other words, you can write a for loop over a channel. This allows for elegant code when working with Tasks and Channels.
Continuing with the producer-consumer example used previously, the values can be consumed via a for loop as follows:
julia> chnl = Channel(producer)Channel{Any}(sz_max:0,sz_curr:1)julia> for i in chnl @show i endi = "start"i = 2i = 4i = 6i = 8i = "stop"
The loop continues until the channel is closed. In this case, the channel we created is closed implicitly when the task is complete, which in turn happens when the producer function returns. When needed, however, the close(chnl) function can be used to explicitly close a channel.
The following code ...
Read now
Unlock full access