用法:
Channel{T=Any}(func::Function, size=0; taskref=nothing, spawn=false)
從 func
創建一個新任務,將其綁定到類型為 T
和大小為 size
的新通道,然後安排任務,所有這些都在一次調用中完成。
func
必須接受綁定通道作為其唯一參數。
如果您需要對創建的任務的引用,請通過關鍵字參數 taskref
傳遞 Ref{Task}
對象。
如果是spawn = true
,那麽為func
創建的Task可以並行調度到另一個線程上,相當於通過
創建一個task。Threads.@spawn
返回 Channel
。
例子
julia> chnl = Channel() do ch
foreach(i -> put!(ch, i), 1:4)
end;
julia> typeof(chnl)
Channel{Any}
julia> for i in chnl
@show i
end;
i = 1
i = 2
i = 3
i = 4
引用創建的任務:
julia> taskref = Ref{Task}();
julia> chnl = Channel(taskref=taskref) do ch
println(take!(ch))
end;
julia> istaskdone(taskref[])
false
julia> put!(chnl, "Hello");
Hello
julia> istaskdone(taskref[])
true
Julia 1.3
spawn=
參數是在 Julia 1.3 中添加的。這個構造函數是在 Julia 1.3 中添加的。在早期版本的 Julia 中,Channel 使用關鍵字參數來設置 size
和 T
,但這些構造函數已被棄用。
julia> chnl = Channel{Char}(1, spawn=true) do ch
for c in "hello world"
put!(ch, c)
end
end
Channel{Char}(1) (1 item available)
julia> String(collect(chnl))
"hello world"
相關用法
- Julia Core.NamedTuple用法及代碼示例
- Julia Core.ifelse用法及代碼示例
- Julia Core.isa用法及代碼示例
- Julia Core.typeof用法及代碼示例
- Julia Core.UnionAll用法及代碼示例
- Julia Core.nfields用法及代碼示例
- Julia Core.Function用法及代碼示例
- Julia Core.DomainError用法及代碼示例
- Julia Core.undef用法及代碼示例
- Julia Core.UndefVarError用法及代碼示例
- Julia Core.isdefined用法及代碼示例
- Julia Core.invoke用法及代碼示例
- Julia Core.Array方法用法及代碼示例
- Julia Core.getfield用法及代碼示例
- Julia Core.ErrorException用法及代碼示例
- Julia Core.Vararg用法及代碼示例
- Julia Core.NTuple用法及代碼示例
- Julia Core.@big_str用法及代碼示例
- Julia Core.Type用法及代碼示例
- Julia Core.Task用法及代碼示例
- Julia Core.Union用法及代碼示例
- Julia Core.AssertionError用法及代碼示例
- Julia Core.Ref用法及代碼示例
- Julia Core.BoundsError用法及代碼示例
- Julia Core.Float64方法用法及代碼示例
注:本文由純淨天空篩選整理自julialang.org 大神的英文原創作品 Base.Channel — Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。