schedule
schedule(t::Task, [val]; error=false)
Add a task to the scheduler's queue. This causes the task to run constantly when the system is otherwise idle, unless the task performs a blocking operation such as wait
.
If a second argument is provided, it will be passed to the task (via the return value of yieldto
) when it runs again. If error
is true
, the value is raised as an exception in the woken task.
Examples
"""
schedule(t::Task, [val]; error=false)
Add a task `t` to the scheduler's queue. This causes the task to run constantly when the system is otherwise idle, unless the task performs a blocking operation such as `wait`.
If a second argument `val` is provided, it will be passed to the task (via the return value of `yieldto`) when it runs again. If `error` is `true`, the value is raised as an exception in the woken task.
"""
# Example 1: Basic usage
task1 = @task println("Task 1 running!")
schedule(task1) # Add task1 to the scheduler's queue
# Example 2: Passing a value to the task
task2 = @task println("Task 2 received value: ", take!())
schedule(task2, "Hello, World!")
# Example 3: Error handling
task3 = @task println("Task 3")
schedule(task3, "Error!", error=true)
# Note: The above examples assume that the tasks are created using `@task` macro and `take!` is used to receive values from a channel.
In the above examples, we demonstrate the usage of the schedule
function in Julia.
-
Basic Usage:
- In Example 1, we create a task
task1
using the@task
macro and add it to the scheduler's queue usingschedule
. The task will run constantly when the system is idle.
- In Example 1, we create a task
-
Passing a value to the task:
- In Example 2, we create a task
task2
and pass the value"Hello, World!"
to it using theschedule
function. The value is received by the task usingtake!
(assumed to be a channel operation).
- In Example 2, we create a task
- Error Handling:
- In Example 3, we create a task
task3
and pass the value"Error!"
to it witherror=true
. This causes the value to be raised as an exception in the task when it runs again.
- In Example 3, we create a task
Note: The examples assume that tasks are created using the @task
macro and take!
is used to receive values from a channel.
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.