Next: , Previous: , Up: actor-lib: A standard library of sorts   [Contents][Index]


6.5 IO

The (goblins actor-lib io) module provides a mechanism for operating on external IO resources in a way which abstracts away Fibers details such that the user can operate mostly in a Goblins-oriented interface.

More specific IO interfaces for files, general ports, etc will be provided in the future, but the mechanism provided by this library is general enough to fit most use cases.

Constructor: ^io _bcom wrapped

[#:init #f] [#:cleanup #f]

Spawns an object which itself spawns a Fibers fiber which wraps wrapped. If init is provided with a procedure, fiber will invoke said procedure with wrapped as the sole argument before looping over any provided commands.

Invoking the spawned object returns a promise, and said procedure is executed within the managed fiber’s loop. Any return value will fulfill as a value for the returned promise, and any exception raised will break the promise with said exception.

If the object spawned by ^io is invoked with the symbol 'halt, the fiber will be shut down and will stop responding to further incoming messages, and cleanup will be run within the fiber with the sole argument being wrapped. (In the future, if the reference to the spawned object is garbage collected, the fiber will be automatically halted.)

As an example:

> (use-modules (goblins)
               (goblins actor-lib io)
               (ice-9 textual-ports))
> (define a-vat (spawn-vat #:name 'a))
> ,enter-vat a-vat
goblins/a [1]> (define wraps-input
                 (spawn ^io (open-input-string "Hello!")
                        ;; burns one character off the top
                        #:init get-char))
goblins/a [1]> (on (<- wraps-input get-char)
                   (lambda (c) (format #t "Got: ~a\n" c)))
Got: e
goblins/a [1]> (on (<- wraps-input get-char)
                   (lambda (c) (format #t "Got: ~a\n" c)))
Got: l
goblins/a [1]> (on (<- wraps-input get-char)
                   (lambda (c) (format #t "Got: ~a\n" c)))
Got: l
goblins/a [1]> (on (<- wraps-input get-char)
                   (lambda (c) (format #t "Got: ~a\n" c)))
Got: o
Constructor: ^read-write-io _bcom wrapped

[#:init #f] [#:cleanup #f]

Spawn a read and write interface for running commands over WRAPPED

Like the ^io object, this spawns a separate fiber which processes one command at a time. This however allows for both reading and writing by having a ’read method (taking in a procedure as its only argument), and a ’write method (also taking a single procedure as its only argument). You can read and write at the same time to the same WRAPPED resource without blocking the other.

Like ^io, this supports a ’halt method which stops both fibers and runs any CLEANUP provided.

Note that currently the io actor is not persistence aware and so cannot appear in a persistence graph.


Next: Joiners, Previous: Inbox, Up: actor-lib: A standard library of sorts   [Contents][Index]