Wrap an expression in a Task without executing it, and return the Task. This only creates a task, and does not run it.


julia> task = @task println("Hello, Julia!")
Task (runnable) @0x0000000114f15870

julia> task
Task (runnable) @0x0000000114f15870

The @task macro is used to wrap an expression in a Task without executing it. It returns the created Task object. The wrapped expression will be executed when the task is scheduled for execution.

Here's an example of how to use the @task macro:

julia> function mytask()
           task = @task begin
               println("Inside the task")
               x = 2 + 3
               println("Result: ", x)
           return task
mytask (generic function with 1 method)

julia> task = mytask()
Task (runnable) @0x0000000114f15870

julia> schedule(task)  # Manually schedule the task for execution
Task (done) @0x0000000114f15870

julia> fetch(task)  # Retrieve the result of the task
Inside the task
Result: 5

In this example, the @task macro is used to wrap a block of code inside a task. The mytask function creates and returns the task. Then, the task is manually scheduled for execution using the schedule function, and the result is retrieved using the fetch function.

Note that the task is not executed immediately upon creation. It is only executed when explicitly scheduled or spawned.

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

Checking you are not a robot: