Skip to content

Commit 01c33e9

Browse files
committed
Replace @async with Threads.@Spawn
1 parent 0377cda commit 01c33e9

File tree

6 files changed

+29
-29
lines changed

6 files changed

+29
-29
lines changed

src/cluster.jl

+16-16
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,10 @@ function check_worker_state(w::Worker)
163163
else
164164
w.ct_time = time()
165165
if myid() > w.id
166-
t = @async exec_conn_func(w)
166+
t = Threads.@spawn exec_conn_func(w)
167167
else
168168
# route request via node 1
169-
t = @async remotecall_fetch((p,to_id) -> remotecall_fetch(exec_conn_func, p, to_id), 1, w.id, myid())
169+
t = Threads.@spawn remotecall_fetch((p,to_id) -> remotecall_fetch(exec_conn_func, p, to_id), 1, w.id, myid())
170170
end
171171
errormonitor(t)
172172
wait_for_conn(w)
@@ -258,7 +258,7 @@ function start_worker(out::IO, cookie::AbstractString=readline(stdin); close_std
258258
else
259259
sock = listen(interface, LPROC.bind_port)
260260
end
261-
errormonitor(@async while isopen(sock)
261+
errormonitor(Threads.@spawn while isopen(sock)
262262
client = accept(sock)
263263
process_messages(client, client, true)
264264
end)
@@ -290,7 +290,7 @@ end
290290

291291

292292
function redirect_worker_output(ident, stream)
293-
t = @async while !eof(stream)
293+
t = Threads.@spawn while !eof(stream)
294294
line = readline(stream)
295295
if startswith(line, " From worker ")
296296
# stdout's of "additional" workers started from an initial worker on a host are not available
@@ -329,7 +329,7 @@ function read_worker_host_port(io::IO)
329329
leader = String[]
330330
try
331331
while ntries > 0
332-
readtask = @async readline(io)
332+
readtask = Threads.@spawn readline(io)
333333
yield()
334334
while !istaskdone(readtask) && ((time_ns() - t0) < timeout)
335335
sleep(0.05)
@@ -430,7 +430,7 @@ if launching workers programmatically, execute `addprocs` in its own task.
430430
431431
```julia
432432
# On busy clusters, call `addprocs` asynchronously
433-
t = @async addprocs(...)
433+
t = Threads.@spawn addprocs(...)
434434
```
435435
436436
```julia
@@ -496,13 +496,13 @@ function addprocs_locked(manager::ClusterManager; kwargs...)
496496
# call manager's `launch` is a separate task. This allows the master
497497
# process initiate the connection setup process as and when workers come
498498
# online
499-
t_launch = @async launch(manager, params, launched, launch_ntfy)
499+
t_launch = Threads.@spawn launch(manager, params, launched, launch_ntfy)
500500

501501
@sync begin
502502
while true
503503
if isempty(launched)
504504
istaskdone(t_launch) && break
505-
@async begin
505+
Threads.@spawn begin
506506
sleep(1)
507507
notify(launch_ntfy)
508508
end
@@ -512,7 +512,7 @@ function addprocs_locked(manager::ClusterManager; kwargs...)
512512
if !isempty(launched)
513513
wconfig = popfirst!(launched)
514514
let wconfig=wconfig
515-
@async setup_launched_worker(manager, wconfig, launched_q)
515+
Threads.@spawn setup_launched_worker(manager, wconfig, launched_q)
516516
end
517517
end
518518
end
@@ -592,7 +592,7 @@ function launch_n_additional_processes(manager, frompid, fromconfig, cnt, launch
592592
wconfig.port = port
593593

594594
let wconfig=wconfig
595-
@async begin
595+
Threads.@spawn begin
596596
pid = create_worker(manager, wconfig)
597597
remote_do(redirect_output_from_additional_worker, frompid, pid, port)
598598
push!(launched_q, pid)
@@ -706,11 +706,11 @@ function create_worker(manager, wconfig)
706706
join_message = JoinPGRPMsg(w.id, all_locs, PGRP.topology, enable_threaded_blas, isclusterlazy())
707707
send_msg_now(w, MsgHeader(RRID(0,0), ntfy_oid), join_message)
708708

709-
errormonitor(@async manage(w.manager, w.id, w.config, :register))
709+
errormonitor(Threads.@spawn manage(w.manager, w.id, w.config, :register))
710710
# wait for rr_ntfy_join with timeout
711711
timedout = false
712712
errormonitor(
713-
@async begin
713+
Threads.@spawn begin
714714
sleep($timeout)
715715
timedout = true
716716
put!(rr_ntfy_join, 1)
@@ -767,7 +767,7 @@ function check_master_connect()
767767
end
768768

769769
errormonitor(
770-
@async begin
770+
Threads.@spawn begin
771771
start = time_ns()
772772
while !haskey(map_pid_wrkr, 1) && (time_ns() - start) < timeout
773773
sleep(1.0)
@@ -1063,13 +1063,13 @@ function rmprocs(pids...; waitfor=typemax(Int))
10631063

10641064
pids = vcat(pids...)
10651065
if waitfor == 0
1066-
t = @async _rmprocs(pids, typemax(Int))
1066+
t = Threads.@spawn _rmprocs(pids, typemax(Int))
10671067
yield()
10681068
return t
10691069
else
10701070
_rmprocs(pids, waitfor)
10711071
# return a dummy task object that user code can wait on.
1072-
return @async nothing
1072+
return Threads.@spawn nothing
10731073
end
10741074
end
10751075

@@ -1252,7 +1252,7 @@ function interrupt(pids::AbstractVector=workers())
12521252
@assert myid() == 1
12531253
@sync begin
12541254
for pid in pids
1255-
@async interrupt(pid)
1255+
Threads.@spawn interrupt(pid)
12561256
end
12571257
end
12581258
end

src/macros.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ function remotecall_eval(m::Module, procs, ex)
230230
# execute locally last as we do not want local execution to block serialization
231231
# of the request to remote nodes.
232232
for _ in 1:run_locally
233-
@async Core.eval(m, ex)
233+
Threads.@spawn Core.eval(m, ex)
234234
end
235235
end
236236
nothing
@@ -275,7 +275,7 @@ function preduce(reducer, f, R)
275275
end
276276

277277
function pfor(f, R)
278-
t = @async @sync for c in splitrange(Int(firstindex(R)), Int(lastindex(R)), nworkers())
278+
t = Threads.@spawn @sync for c in splitrange(Int(firstindex(R)), Int(lastindex(R)), nworkers())
279279
@spawnat :any f(R, first(c), last(c))
280280
end
281281
errormonitor(t)

src/managers.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ function launch(manager::SSHManager, params::Dict, launched::Array, launch_ntfy:
176176
# Wait for all launches to complete.
177177
@sync for (i, (machine, cnt)) in enumerate(manager.machines)
178178
let machine=machine, cnt=cnt
179-
@async try
179+
Threads.@spawn try
180180
launch_on_machine(manager, $machine, $cnt, params, launched, launch_ntfy)
181181
catch e
182182
print(stderr, "exception launching on machine $(machine) : $(e)\n")
@@ -742,7 +742,7 @@ function kill(manager::LocalManager, pid::Int, config::WorkerConfig; exit_timeou
742742
# First, try sending `exit()` to the remote over the usual control channels
743743
remote_do(exit, pid)
744744

745-
timer_task = @async begin
745+
timer_task = Threads.@spawn begin
746746
sleep(exit_timeout)
747747

748748
# Check to see if our child exited, and if not, send an actual kill signal

src/messages.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ function flush_gc_msgs()
200200
end
201201
catch e
202202
bt = catch_backtrace()
203-
@async showerror(stderr, e, bt)
203+
Threads.@spawn showerror(stderr, e, bt)
204204
end
205205
end
206206

src/process_messages.jl

+7-7
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ function schedule_call(rid, thunk)
8585
rv = RemoteValue(def_rv_channel())
8686
(PGRP::ProcessGroup).refs[rid] = rv
8787
push!(rv.clientset, rid.whence)
88-
errormonitor(@async run_work_thunk(rv, thunk))
88+
errormonitor(Threads.@spawn run_work_thunk(rv, thunk))
8989
return rv
9090
end
9191
end
@@ -118,7 +118,7 @@ end
118118

119119
## message event handlers ##
120120
function process_messages(r_stream::TCPSocket, w_stream::TCPSocket, incoming::Bool=true)
121-
errormonitor(@async process_tcp_streams(r_stream, w_stream, incoming))
121+
errormonitor(Threads.@spawn process_tcp_streams(r_stream, w_stream, incoming))
122122
end
123123

124124
function process_tcp_streams(r_stream::TCPSocket, w_stream::TCPSocket, incoming::Bool)
@@ -148,7 +148,7 @@ Julia version number to perform the authentication handshake.
148148
See also [`cluster_cookie`](@ref).
149149
"""
150150
function process_messages(r_stream::IO, w_stream::IO, incoming::Bool=true)
151-
errormonitor(@async message_handler_loop(r_stream, w_stream, incoming))
151+
errormonitor(Threads.@spawn message_handler_loop(r_stream, w_stream, incoming))
152152
end
153153

154154
function message_handler_loop(r_stream::IO, w_stream::IO, incoming::Bool)
@@ -283,7 +283,7 @@ function handle_msg(msg::CallMsg{:call}, header, r_stream, w_stream, version)
283283
schedule_call(header.response_oid, ()->invokelatest(msg.f, msg.args...; msg.kwargs...))
284284
end
285285
function handle_msg(msg::CallMsg{:call_fetch}, header, r_stream, w_stream, version)
286-
errormonitor(@async begin
286+
errormonitor(Threads.@spawn begin
287287
v = run_work_thunk(()->invokelatest(msg.f, msg.args...; msg.kwargs...), false)
288288
if isa(v, SyncTake)
289289
try
@@ -299,15 +299,15 @@ function handle_msg(msg::CallMsg{:call_fetch}, header, r_stream, w_stream, versi
299299
end
300300

301301
function handle_msg(msg::CallWaitMsg, header, r_stream, w_stream, version)
302-
errormonitor(@async begin
302+
errormonitor(Threads.@spawn begin
303303
rv = schedule_call(header.response_oid, ()->invokelatest(msg.f, msg.args...; msg.kwargs...))
304304
deliver_result(w_stream, :call_wait, header.notify_oid, fetch(rv.c))
305305
nothing
306306
end)
307307
end
308308

309309
function handle_msg(msg::RemoteDoMsg, header, r_stream, w_stream, version)
310-
errormonitor(@async run_work_thunk(()->invokelatest(msg.f, msg.args...; msg.kwargs...), true))
310+
errormonitor(Threads.@spawn run_work_thunk(()->invokelatest(msg.f, msg.args...; msg.kwargs...), true))
311311
end
312312

313313
function handle_msg(msg::ResultMsg, header, r_stream, w_stream, version)
@@ -350,7 +350,7 @@ function handle_msg(msg::JoinPGRPMsg, header, r_stream, w_stream, version)
350350
# The constructor registers the object with a global registry.
351351
Worker(rpid, ()->connect_to_peer(cluster_manager, rpid, wconfig))
352352
else
353-
@async connect_to_peer(cluster_manager, rpid, wconfig)
353+
Threads.@spawn connect_to_peer(cluster_manager, rpid, wconfig)
354354
end
355355
end
356356
end

src/remotecall.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ or to use a local [`Channel`](@ref) as a proxy:
205205
```julia
206206
p = 1
207207
f = Future(p)
208-
errormonitor(@async put!(f, remotecall_fetch(long_computation, p)))
208+
errormonitor(Threads.@spawn put!(f, remotecall_fetch(long_computation, p)))
209209
isready(f) # will not block
210210
```
211211
"""

0 commit comments

Comments
 (0)