wait

wait([x])

Block the current task until some event occurs, depending on the type of the argument:

  • RemoteRef: Wait for a value to become available for the specified remote reference.
  • Channel: Wait for a value to be appended to the channel.
  • Condition: Wait for notify on a condition.
  • Process: Wait for a process or process chain to exit. The exitcode field of a process can be used to determine success or failure.
  • Task: Wait for a Task to finish, returning its result value. If the task fails with an exception, the exception is propagated (re-thrown in the task that called wait).
  • RawFD: Wait for changes on a file descriptor (see poll_fd for keyword arguments and return code)

If no argument is passed, the task blocks for an undefined period. If the task's state is set to :waiting, it can only be restarted by an explicit call to schedule or yieldto. If the task's state is :runnable, it might be restarted unpredictably.

Often wait is called within a while loop to ensure a waited-for condition is met before proceeding.

Examples

wait([x])

Block the current task until some event occurs, depending on the type of the argument.

  • RemoteRef: Wait for a value to become available for the specified remote reference.

    julia> @spawn begin
             sleep(2)
             42
         end |> wait
    42
  • Channel: Wait for a value to be appended to the channel.

    julia> channel = Channel{Int}(1)
    Channel{Int64}(sz_max:1,sz_curr:0)
    
    julia> @async begin
             sleep(2)
             put!(channel, 42)
         end
    
    julia> wait(channel)
    42
  • Condition: Wait for notify on a condition.

    julia> condition = Condition()
    Base.Threads.Condition(0x0000000000000000, Base.Threads.SpinLock(0x0000000000000000))
    
    julia> @async begin
             sleep(2)
             notify(condition)
         end
    
    julia> wait(condition)
  • Process: Wait for a process or process chain to exit. The exitcode field of a process can be used to determine success or failure.

    julia> process = run(`echo "Hello, Julia!"`)
    Process(`echo "Hello, Julia!"`, ProcessExited(0))
    
    julia> wait(process)
    0
  • Task: Wait for a Task to finish, returning its result value. If the task fails with an exception, the exception is propagated (re-thrown in the task that called wait).

    julia> task = @async begin
             sleep(2)
             42
         end
    
    julia> wait(task)
    42
  • RawFD: Wait for changes on a file descriptor (see poll_fd for keyword arguments and return code).

    julia> fd = open("data.txt", "r")
    IOStream(<file data.txt>)
    
    julia> wait(fd)

If no argument is passed, the task blocks for an undefined period. If the task's state is set to :waiting, it can only be restarted by an explicit call to schedule or yieldto. If the task's state is :runnable, it might be restarted unpredictably.

Often wait is called within a while loop to ensure a waited-for condition is met before proceeding.

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: