Previous: , Up: Scripting   [Contents][Index]


2.4.4 Channels

Channels are a tool for communicating amongst different scripts. One script can write a value to the channel and another can read from it. Reading or writing to a channel suspends that script until there is someone on the other end of the line to complete the transaction.

Here’s a simplistic example:

(define c (make-channel))

(script
 (forever
  (let ((item (channel-get c)))
    (pk 'got item))))

(script
 (channel-put c 'sword)
 (channel-put c 'shield)
 (channel-put c 'potion))
Procedure: make-channel

Return a new channel

Procedure: channel? obj

Return #t if obj is a channel.

Procedure: channel-get channel

Retrieve a value from channel. The current script suspends until a value is available.

Procedure: channel-put channel data

Send data to channel. The current script suspends until another script is available to retrieve the value.

A low-level API also exists for using channels outside of a script via callback procedures:

Procedure: channel-get! channel proc

Asynchronously retrieve a value from channel and call proc with that value.

Procedure: channel-put! channel data [thunk]

Asynchronously send data to channel and call thunk after it has been received.