Skip to content

Commit 87c2162

Browse files
authored
Fix @spawn_or_run_task with interactive threads (#3385)
When Julia ≥ 1.9 is started with `-tX,Y`, by default tasks use the pool of interactive threads instead of the default pool. Fix this by updating `spawn_or_run_task` and `spawn_or_run` to match the code used by `@spawn` in Julia master.
1 parent b51a973 commit 87c2162

File tree

4 files changed

+30
-8
lines changed

4 files changed

+30
-8
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ jobs:
3636
- uses: julia-actions/julia-buildpkg@v1
3737
- uses: julia-actions/julia-runtest@v1
3838
env:
39-
JULIA_NUM_THREADS: 4
39+
JULIA_NUM_THREADS: 4,1
4040
- uses: julia-actions/julia-processcoverage@v1
4141
- uses: codecov/codecov-action@v1
4242
with:

NEWS.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@
1010
keyword argument
1111
([#3380](https://github.com/JuliaData/DataFrames.jl/pull/3380))
1212

13+
## Bug fixes
14+
15+
* Always use the default thread pool for multithreaded operations,
16+
instead of using the interactive thread pool when Julia was started
17+
with `-tM,N` with N > 0
18+
([#3385](https://github.com/JuliaData/DataFrames.jl/pull/3385))
19+
1320
# DataFrames.jl v1.6.1 Release Notes
1421

1522
## Bug fixes

docs/src/lib/functions.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ CurrentModule = DataFrames
77
## Multithreading support
88

99
By default, selected operations in DataFrames.jl automatically use multiple threads
10-
when available. It is task-based and implemented using the `@spawn` macro from Julia Base.
10+
when available. Multi-threading is task-based and implemented using the `@spawn`
11+
macro from Julia Base. Tasks are therefore scheduled on the `:default` threadpool.
1112
Functions that take user-defined functions and may run it in parallel
1213
accept a `threads` keyword argument which allows disabling multithreading
1314
when the provided function requires serial execution or is not thread-safe.

src/other/utils.jl

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -221,16 +221,23 @@ end
221221
Equivalent to `Threads.@spawn` if `threads === true`,
222222
otherwise run `expr` and return a `Task` that returns its value.
223223
"""
224-
macro spawn_or_run_task(threads, expr)
225-
letargs = Base._lift_one_interp!(expr)
224+
macro spawn_or_run_task(threads, ex)
225+
letargs = Base._lift_one_interp!(ex)
226226

227-
thunk = esc(:(()->($expr)))
227+
thunk = :(()->($(esc(ex))))
228+
@static if VERSION >= v"1.10.0-DEV"
229+
Base.replace_linenums!(thunk, __source__)
230+
end
228231
var = esc(Base.sync_varname)
232+
spawn_set_thrpool = VERSION >= v"1.9.0" ?
233+
:(Base.Threads._spawn_set_thrpool(task, :default)) :
234+
:()
229235
quote
230236
let $(letargs...)
231237
if $(esc(threads))
232238
local task = Task($thunk)
233239
task.sticky = false
240+
$(spawn_set_thrpool)
234241
else
235242
# Run expr immediately
236243
res = $thunk()
@@ -253,16 +260,23 @@ end
253260
Equivalent to `Threads.@spawn` if `threads === true`,
254261
otherwise run `expr`.
255262
"""
256-
macro spawn_or_run(threads, expr)
257-
letargs = Base._lift_one_interp!(expr)
263+
macro spawn_or_run(threads, ex)
264+
letargs = Base._lift_one_interp!(ex)
258265

259-
thunk = esc(:(()->($expr)))
266+
thunk = :(()->($(esc(ex))))
267+
if VERSION >= v"1.10.0-DEV"
268+
Base.replace_linenums!(thunk, __source__)
269+
end
260270
var = esc(Base.sync_varname)
271+
spawn_set_thrpool = VERSION >= v"1.9.0" ?
272+
:(Base.Threads._spawn_set_thrpool(task, :default)) :
273+
:()
261274
quote
262275
let $(letargs...)
263276
if $(esc(threads))
264277
local task = Task($thunk)
265278
task.sticky = false
279+
$(spawn_set_thrpool)
266280
if $(Expr(:islocal, var))
267281
put!($var, task)
268282
end

0 commit comments

Comments
 (0)