Skip to content

Commit fb31d7f

Browse files
committed
Add macro mp_async
1 parent b29f3f1 commit fb31d7f

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

src/onprocs.jl

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,31 @@ end
3333
export @onprocs
3434

3535

36+
"""
37+
@mp_async expr
38+
39+
Run `expr` asynchronously on a worker process.
40+
41+
Compatible with `@sync`.
42+
43+
Equivalent to `Distributed.@spawn expr` on Julia <= v1.2, equivalent to
44+
`Distributed.@spawn :any expr` on Julia >= v1.3.
45+
"""
46+
macro mp_async(expr)
47+
# Code taken from Distributed.@spawn:
48+
thunk = esc(:(()->($expr)))
49+
var = esc(Base.sync_varname)
50+
quote
51+
local ref = Distributed.spawn_somewhere($thunk)
52+
if $(Expr(:isdefined, var))
53+
push!($var, ref)
54+
end
55+
ref
56+
end
57+
end
58+
export @mp_async
59+
60+
3661
function mtjulia_exe()
3762
if Sys.islinux()
3863
joinpath(@__DIR__, "..", "bin", "mtjulia.sh")

test/test_onprocs.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,20 @@ using Distributed
3030
end) == ref_result
3131
end
3232

33+
@testset "macro mp_async" begin
34+
@test begin
35+
n = 128
36+
A = Vector{Future}(undef, n)
37+
@sync for i in 1:n
38+
A[i] = @mp_async begin
39+
@assert myid() != 1
40+
log(i)
41+
end
42+
end
43+
fetch.(A) == log.(1:n)
44+
end
45+
end
46+
3347
@testset "mtjulia_exe" begin
3448
if Sys.islinux()
3549
@test fetch(@spawnat first(workers()) nthreads()) > 1

0 commit comments

Comments
 (0)