StableTasks is a simple package with one main API StableTasks.@spawn
(not exported by default).
It works like Threads.@spawn
, except it is type stable to fetch
from.
julia> using StableTasks, Test
julia> @inferred fetch(StableTasks.@spawn 1 + 1)
2
versus
julia> @inferred fetch(Threads.@spawn 1 + 1)
ERROR: return type Int64 does not match inferred return type Any
Stacktrace:
[1] error(s::String)
@ Base ./error.jl:35
[2] top-level scope
@ REPL[3]:1
The package also provides StableTasks.@spawnat
(not exported), which is similar to StableTasks.@spawn
but creates a sticky task (it won't migrate) on a specific thread.
julia> t = StableTasks.@spawnat 4 Threads.threadid();
julia> @inferred fetch(t)
4
For convenience, and similar to at Distributed.jl, there are also @fetch
and @fetchfrom
macros:
julia> StableTasks.@fetch 3+3
6
julia> StableTasks.@fetchfrom 2 Threads.threadid()
2