Help understanding example dealing with @async/wait/yield/schedule in Redefining Methods section




I’m reading the Redefining Methods section (, and I don’t understand it properly.

The lines that give me some trouble are:

julia> f(x::Int) = "definition for Int"
julia> t = @async f(wait()); yield();
julia> wait(schedule(t, 1))

And here the questions:

  1. Do we need yield() in the definition of t? As I understand it yield() will just return to the scheduler, but since this is the las instruction for t, just removing this line will end the execution of t and also return to the scheduler, right?

  2. wait() will block the current task until woken up by schedule. Just to make sure, the task is actually the @async block, right? I mean, it is not stopping inside f, but just before the actual call to f is made?

  3. In wait(schedule(t,1)), why do we need the wait? Trying the code in the REPL I see that if we only issue schedule(t,1), the task is finished, but I don’t get the “definition for Int” string. So, for tasks, if we want to keep the returned value, we always need to use wait?

  4. And the last one. The 1 in schedule(t,1) is passed to the function f, but I don’t understand the syntax properly. The documentation for schedule says: If a second argument val is provided, it will be passed to the task (via the return value of yieldto) when it runs again. So, I guess this is basically the same as the previous point: schedule passes number 1 to the task t, which was wait-ing, so this is the return value of wait(), which is passed to the function f.

Such a small code, so many questions :slight_smile:


It’s after the definition, not in it, so this starts t running.