Task not runnable error

Hi folks, I have a task that says it’s runnable, but I get a task not runnable error:

julia> task = @task readline(stdin)
Task (runnable) @0x000000010f798010

julia> yield(task)

julia> task
Task (runnable) @0x000000010f798010

julia> yield(task)
ERROR: yield: Task not runnable
 [1] error(s::String)
   @ Base ./error.jl:33
 [2] yield (repeats 2 times)
   @ ./task.jl:737 [inlined]
 [3] top-level scope
   @ REPL[4]:1

julia> task
Task (runnable) @0x000000010f798010

What is going on here?

I’d say the printing of the task is misleading here. (runnable) does not mean “the user can schedule it.”

Also, I think it’s very tricky to use yield for a task that is already started. In particular, the code in the OP is not correct. If you want to know when yield can be called on the task that is already started, have a look at RFC: Low-level API for `schedule` and `wait` by tkf · Pull Request #41270 · JuliaLang/julia · GitHub which discusses when schedule can be used for the task that is already started. Using yield is even more strict since you have to know that the context switch has been completed. I think this requires knowing the worker thread that executed the said task and then that the worker thread is now executing another task.

In short: I don’t think you’d want to do this.

1 Like

I think the zero argument yield() proved the best solution for my use case.

Yes, zero-arg yield() is a good API for telling the scheduler to try other tasks.