REBOL 3 Docs Guide Concepts Functions Datatypes Errors
  TOC < Back Next >   Updated: 6-Feb-2009 Edit History  

REBOL 3 Concepts: Ports: Waiting for a Port

Pending Revision

This document was written for R2 and has yet to be revised for R3.

The wait function is essential to programs that need to handle asynchronous data transfers. With wait, you can wait for data on one or more ports, or for a timeout to occur.

The wait function will accept a single port:

wait port

or, an entire block of ports can be provided:

wait [port1 port2 port3]

In addition, a timeout value can be provided as a number of seconds or as a time value:

wait [port1 port2 10]

wait [port1 port2 0:00:05]

The first example will time out in ten seconds. The second example will timeout in five seconds.

The wait function will return the port that is ready or none! if the timeout occurred.

ready: wait [port1 port2 10]
if ready [data: copy ready]

The above example will read data from the first ready port if a timeout did not occur.

To obtain a block of all ports that are ready, use the /all refinement.

ready: wait/all [port1 port2 10]
if ready [
    foreach port ready [
        append data copy port

This example would append data from all ready ports into a single series.

You can also use the [bad-link:functions/dispatch.txt] function to evaluate a block or function based on the results of a wait on multiple ports.

dispatch [
    port1 [print "port1 awake"]
    port2 [print "port2 awake"]
    10 [print "timeout!"]
Use /No-wait and /Direct

To use wait with most ports, you will need to specify the /no-wait and /direct refinements as part of the open. This indicates that the normal data access functions should not block and that data is not buffered.

port1: open/no-wait/direct tcp://system:8000

  TOC < Back Next > - WIP Wiki Feedback Admin