When using Julia v1.7.2, I get a TaskFailedException when trying to use the @threads macro on a for loop over a dictionary, even though a non-threaded for loop works correctly. I lock the DataFrame that is written to. Does anyone see what I’m doing wrong? Here’s my code:
using DataFrames
txt = "Let the stormy clouds chase, everyone from the place."
wds = split(txt, r"\W+", keepempty=false)
wds_dict = Dict(w=>i for (i, w) in enumerate(wds))
df = DataFrame(wd = String[], index = Int64[])
lk = ReentrantLock()
#for (w, i) in wds_dict # this for loop work correctly
Threads.@threads for (w, i) in wds_dict
lock(lk) do
push!(df, [w i])
end
end
println(df)
println(Threads.nthreads())
And here’s the error message:
ERROR: LoadError: TaskFailedException
Stacktrace:
[1] wait
@ ./task.jl:334 [inlined]
[2] threading_run(func::Function)
@ Base.Threads ./threadingconstructs.jl:38
[3] top-level scope
@ ./threadingconstructs.jl:97
nested task error: MethodError: no method matching firstindex(::Dict{SubString{String}, Int64})
Closest candidates are:
firstindex(::Any, ::Any) at ~/Downloads/julia-1.7.2/share/julia/base/abstractarray.jl:396
firstindex(::Union{Tables.AbstractColumns, Tables.AbstractRow}) at ~/.julia/packages/Tables/PxO1m/src/Tables.jl:177
firstindex(::DataFrames.DataFrameColumns) at ~/.julia/packages/DataFrames/MA4YO/src/abstractdataframe/iteration.jl:189
...
Stacktrace:
[1] (::var"#4#threadsfor_fun#4"{Dict{SubString{String}, Int64}})(onethread::Bool)
@ Main ./threadingconstructs.jl:70
[2] (::var"#4#threadsfor_fun#4"{Dict{SubString{String}, Int64}})()
@ Main ./threadingconstructs.jl:52
in expression starting at /home/earl/Downloads/delete.jl:10