JuliaFolds2 / OhMyThreads.jl

Simple multithreading in julia
https://juliafolds2.github.io/OhMyThreads.jl/
MIT License
130 stars 8 forks source link

`@one_by_one` doesn't work inside a `if`-block #118

Open Moelf opened 3 days ago

Moelf commented 3 days ago
julia> @tasks for i in 1:10
           if true
               @one_by_one begin
                   println(i)
                   sleep(0.1)
               end
           end
       end
ERROR: LoadError: The @one_by_one macro may only be used inside of a @tasks block.
Stacktrace:
 [1] error(s::String)
   @ Base ./error.jl:35
 [2] var"@one_by_one"(__source__::LineNumberNode, __module__::Module, args::Vararg{Any})
   @ OhMyThreads ~/.julia/packages/OhMyThreads/20X0i/src/macros.jl:207
in expression starting at REPL[2]:3

Currently the @tasks macro needs to see @one_by_one directly, but logically speaking this is not necessary

carstenbauer commented 3 days ago

We need to recursively look for our special macros. Probably using MacroTools is easiest.

carstenbauer commented 3 days ago

We only cared about “top-level” (relative to @tasks) because we started with @set and @local and those only make sense there. Supporting @one_by_one in (potentially nested) if(s) is harder than I thought. It doesn’t fit into the scheme we currently use to find and replace those macros.

As a workaround you can use:

julia> @tasks for i in 1:10
           if true
               lock(lck) do
                   println(i)
                   sleep(0.1)
               end
           end
       end
3
10
7
9
5
6
4
8
1
2