[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Guile fibers return values
From: |
Zelphir Kaltstahl |
Subject: |
Re: Guile fibers return values |
Date: |
Sun, 5 Jan 2020 02:30:06 +0100 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Icedove/52.9.1 |
Hello Guile Users,
so I figured out an example for using channels, but I am not sure, if
that is the only way to get results from a fiber:
~~~~8<~~~~8<~~~~
(use-modules
(fibers)
(fibers channels)
(ice-9 match))
;; Define a procedure to run in a fiber.
(define fiber1-proc
(lambda (in-chan out-chan)
;; Look for mesages.
(let loop ([received-proc (lambda (data) 'no-proc-received)])
(match
;; Write arguments to the current output port, and return the last
;; argument. This will get the message received and write to current
;; output port an information, that the get-message procedure was
;; called.
(pk 'fiber1-proc-called-get-message
(get-message in-chan))
;; Match anything tagged as procedure and store it in the argument for
;; the named let.
[('proc . proc)
(loop proc)]
;; Match anything labeled as data and apply the stored procedure to
;; it. If no procedure has been received yet, use the default one.
[('data . data)
(put-message out-chan (received-proc data))
;; Loop again with the default procedure, awaiting a new procedure and
;; data for it.
(loop (lambda (data) 'no-proc-received))]
;; Have a default reaction to anything, but the correctly tagged
;; messages.
[any-other-message
(put-message out-chan 'unrecognized-message)
;; Allow for unrecognized messages in between correct communication.
(loop received-proc)])
;; Continue looking for messages.
(loop received-proc))))
(run-fibers
(lambda ()
(let ((fiber1-in-chan (make-channel))
(fiber1-out-chan (make-channel)))
;; Spawn a fiber to run fiber1-proc, which internally looks for messages on
;; its in-channel.
(spawn-fiber
(lambda ()
(fiber1-proc fiber1-in-chan fiber1-out-chan)))
;; Send a mssage to the fiber.
(put-message fiber1-in-chan
;; Send some tagged data, in this case the procedure to use.
(cons 'proc
;; A procedure, which checks all things in data for
;; whether they are even numbers and builds a list of
;; the answers.
(lambda (data)
(let loop ([remaining-data data])
(cond
[(null? remaining-data) '()]
[else
(cons (even? (car remaining-data))
(loop (cdr remaining-data)))])))))
;; Then put the data on the channel.
(put-message fiber1-in-chan
(cons 'data '(0 1 2 3 4 5 6 7 8 9)))
;; Look for the answer on the out-channel of the fiber.
(display
(simple-format
#f "~a\n" (pk 'main-thread-called-peek
(get-message fiber1-out-chan))))
;; And then do it again.
;; Send a mssage to the fiber.
(put-message fiber1-in-chan
;; Send some tagged data, in this case the procedure to use.
(cons 'proc
;; A procedure, which checks all things in data for
;; whether they are even numbers and builds a list of
;; the answers.
(lambda (data)
(let loop ([remaining-data data])
(cond
[(null? remaining-data) '()]
[else
(cons (even? (car remaining-data))
(loop (cdr remaining-data)))])))))
;; Then put the data on the channel.
(put-message fiber1-in-chan
(cons 'data '(0 1 2 3 4 5 6 7 8 9)))
;; Look for the answer on the out-channel of the fiber.
(display
(simple-format
#f "~a\n" (pk 'main-thread-called-peek
(get-message fiber1-out-chan)))))))
~~~~>8~~~~>8~~~~
Is there another way or anything quite wrong in this example?
This way of communication between the fiber and the main process seems
in the style of Racket's places. Except that I can send normal
procedures / lambdas to the fiber, which is great on a single machine,
while I need to send serializable lambdas to Racket places (and I have
not gotten to do that yet).
Is there a restriction on the kind of lambdas I can send on a channel as
I did in the example above?
Regards,
Zelphir
Regards,
Zelphir
- Guile fibers return values, Zelphir Kaltstahl, 2020/01/04
- Re: Guile fibers return values,
Zelphir Kaltstahl <=
- Re: Guile fibers return values, Chris Vine, 2020/01/05
- Re: Guile fibers return values, Zelphir Kaltstahl, 2020/01/05
- Re: Guile fibers return values, Chris Vine, 2020/01/05
- Message not available
- Re: Guile fibers return values, Zelphir Kaltstahl, 2020/01/05
- Re: Guile fibers return values, Chris Vine, 2020/01/05
- Re: Guile fibers return values, Zelphir Kaltstahl, 2020/01/06
- Re: Guile fibers return values, Chris Vine, 2020/01/06
- Re: Guile fibers return values, John Cowan, 2020/01/06
- Re: Guile fibers return values, Zelphir Kaltstahl, 2020/01/06
- Re: Guile fibers return values, John Cowan, 2020/01/06