notify
notify(condition, val=nothing; all=true, error=false)
Wake up tasks waiting for a condition, passing them val. If all is true (the default), all waiting tasks are woken, otherwise only one is. If error is true, the passed value is raised as an exception in the woken tasks.
Examples
-
Wake up all waiting tasks with a value:
julia> condition = Condition(); julia> task1 = @async begin wait(condition); println("Task 1 executed"); end; julia> task2 = @async begin wait(condition); println("Task 2 executed"); end; julia> notify(condition, "Value", all=true, error=false); Task 1 executed Task 2 executedIn this example, both
task1andtask2are waiting on thecondition. Whennotifyis called withall=trueandval="Value", both tasks wake up and execute. -
Wake up a single waiting task with a value:
julia> condition = Condition(); julia> task1 = @async begin wait(condition); println("Task 1 executed"); end; julia> task2 = @async begin wait(condition); println("Task 2 executed"); end; julia> notify(condition, "Value", all=false, error=false); Task 1 executedIn this example, only
task1is woken up and executed becauseall=falsewas specified in thenotifycall. - Wake up tasks and raise an exception:
julia> condition = Condition(); julia> task1 = @async begin wait(condition); println("Task 1 executed"); end; julia> task2 = @async begin wait(condition); println("Task 2 executed"); end; julia> notify(condition, ArgumentError("Invalid argument"), all=true, error=true); ERROR: ArgumentError: Invalid argument ...Here, both tasks are woken up by calling
notifywithall=true, and anArgumentErrorexception is raised in each task.
Common mistake example:
julia> condition = Condition();
julia> notify(condition, "Value", all=1, error=false);
ERROR: MethodError: no method matching notify(::Condition, ::String; all=1, error=false)
In this example, the all argument is given a value of 1 instead of a boolean value (true or false). Ensure that the all argument is provided with the correct boolean value to avoid such errors.
See Also
:@async, :@schedule, :@task, Condition, consume, interrupt, istaskdone, istaskstarted, lock, notify, ReentrantLock, schedule, Task, task_local_storage, unlock, wait, yield, yieldto,User Contributed Notes
Add a Note
The format of note supported is markdown, use triple backtick to start and end a code block.