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.

  1. Basic Usage:

    • In Example 1, we create a task task1 using the @task macro and add it to the scheduler's queue using schedule. The task will run constantly when the system is idle.
  2. Passing a value to the task:

    • In Example 2, we create a task task2 and pass the value "Hello, World!" to it using the schedule function. The value is received by the task using take! (assumed to be a channel operation).
  3. Error Handling:
    • In Example 3, we create a task task3 and pass the value "Error!" to it with error=true. This causes the value to be raised as an exception in the task when it runs again.

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.

*Required Field
Details

Checking you are not a robot: