unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* GOOPS and fibers - need help understanding what's wrong, bug in fibers/guile?
@ 2020-07-21 23:56 Jan Wielkiewicz
  2020-07-22 11:54 ` Chris Vine
  0 siblings, 1 reply; 4+ messages in thread
From: Jan Wielkiewicz @ 2020-07-21 23:56 UTC (permalink / raw)
  To: guile-user

Hello,

I started writing my project in Guile with GOOPS and fibers (I'll
release it once it stops being a shame and starts working!), but I
encountered a problem:

The example from fibers' manual doesn't work:

(lambda ()
 (spawn-fiber (lambda () (display "hey!\n")))))

It doesn't print "hey" as documented in the manual.
I use guile 3.0.4 and fibers 1.0.0 (from Guix).

I also tried making a simple proof of concept, but it doesn't seem to
do anything. I would like to understand what's wrong with my code or
report a bug, if this is the case. No errors, warnings, syntax errors,
just silence.

My code:

(define-module (blocks block)
  #:use-module (fibers)
  #:use-module (fibers channels)
  #:use-module (oop goops)
  #:use-module (srfi srfi-9) ;; records
  #:export (<peer>
            connect-peers
            send-message
            handle-message
            start-listening))

(define-class <peer> ()
  (input #:init-form '() #:getter get-input
         #:setter set-input #:init-keyword #:input)
  (output #:init-form '() #:getter get-output
          #:setter set-output #:init-keyword #:output))

;; Only connection, no messaging started.
(define-method (connect-peers (p1 <peer>) (p2 <peer>))
  (let ((p1-p2 (make-channel)) ;; p1 to p2
        (p2-p1 (make-channel))) ;; p2 to p1
    (set-input p1 p2-p1)
    (set-output p1 p1-p2)
    (set-input p2 p1-p2)
    (set-output p2 p2-p1)))

(define-method (send-message (p <peer>) msg)
  (put-message (get-output p) msg))

(define-method (handle-message (p <peer>))
  (let loop ()
    (define msg (get-message (get-input p)))
    (match (pk msg)
           ('ping (send-message p 'pong)))
    (loop)))

(define-method (start-listening (p1 <peer>) (p2 <peer>))
  (run-fibers
   (lambda ()
     (spawn-fiber
      (lambda ()
        (connect-peers p1 p2)
        (handle-message p1)
        (send-message p1 'ping)
        (handle-message p2))))))

I run this in repl:

(define p1 (make <peer>))
(define p2 (make <peer>))

(start-listening p1 p2)


-----

Thanks in advance,
Jan Wielkiewicz



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: GOOPS and fibers - need help understanding what's wrong, bug in fibers/guile?
  2020-07-21 23:56 GOOPS and fibers - need help understanding what's wrong, bug in fibers/guile? Jan Wielkiewicz
@ 2020-07-22 11:54 ` Chris Vine
  2020-07-22 12:28   ` Chris Vine
  0 siblings, 1 reply; 4+ messages in thread
From: Chris Vine @ 2020-07-22 11:54 UTC (permalink / raw)
  To: guile-user

On Wed, 22 Jul 2020 01:56:53 +0200
Jan Wielkiewicz <tona_kosmicznego_smiecia@interia.pl> wrote:
> Hello,
> 
> I started writing my project in Guile with GOOPS and fibers (I'll
> release it once it stops being a shame and starts working!), but I
> encountered a problem:
> 
> The example from fibers' manual doesn't work:
> 
> (lambda ()
>  (spawn-fiber (lambda () (display "hey!\n")))))
> 
> It doesn't print "hey" as documented in the manual.
> I use guile 3.0.4 and fibers 1.0.0 (from Guix).

(You are missing '(run-fibers' at the beginnng.  Presumably this was
just a pasting error on your part.)

With that omission corrected, it used to work with guile-2.2.  Something
seems to have changed with guile-3.0, because it only works for me with
guile-3.0 if you set #:drain? to #t in the call to run-fibers.  Without
that, run-fibers appears to return before init-thunk returns.

So it seems that there is either a bug in the manual or in the fibers
implementation, probably the latter.

> I also tried making a simple proof of concept, but it doesn't seem to
> do anything. I would like to understand what's wrong with my code or
> report a bug, if this is the case. No errors, warnings, syntax errors,
> just silence.
> 
> My code:
> 
> (define-module (blocks block)
>   #:use-module (fibers)
>   #:use-module (fibers channels)
>   #:use-module (oop goops)
>   #:use-module (srfi srfi-9) ;; records
>   #:export (<peer>
>             connect-peers
>             send-message
>             handle-message
>             start-listening))
> 
> (define-class <peer> ()
>   (input #:init-form '() #:getter get-input
>          #:setter set-input #:init-keyword #:input)
>   (output #:init-form '() #:getter get-output
>           #:setter set-output #:init-keyword #:output))
> 
> ;; Only connection, no messaging started.
> (define-method (connect-peers (p1 <peer>) (p2 <peer>))
>   (let ((p1-p2 (make-channel)) ;; p1 to p2
>         (p2-p1 (make-channel))) ;; p2 to p1
>     (set-input p1 p2-p1)
>     (set-output p1 p1-p2)
>     (set-input p2 p1-p2)
>     (set-output p2 p2-p1)))
> 
> (define-method (send-message (p <peer>) msg)
>   (put-message (get-output p) msg))
> 
> (define-method (handle-message (p <peer>))
>   (let loop ()
>     (define msg (get-message (get-input p)))
>     (match (pk msg)
>            ('ping (send-message p 'pong)))
>     (loop)))
> 
> (define-method (start-listening (p1 <peer>) (p2 <peer>))
>   (run-fibers
>    (lambda ()
>      (spawn-fiber
>       (lambda ()
>         (connect-peers p1 p2)
>         (handle-message p1)
>         (send-message p1 'ping)
>         (handle-message p2))))))
> 
> I run this in repl:
> 
> (define p1 (make <peer>))
> (define p2 (make <peer>))
> 
> (start-listening p1 p2)

TL;DR, and I do not either like or use GOOPS, so I am not sure what you
are doing anyway. But try setting #:drain? to #t.

Chris



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: GOOPS and fibers - need help understanding what's wrong, bug in fibers/guile?
  2020-07-22 11:54 ` Chris Vine
@ 2020-07-22 12:28   ` Chris Vine
  2020-07-24 16:19     ` Jan Wielkiewicz
  0 siblings, 1 reply; 4+ messages in thread
From: Chris Vine @ 2020-07-22 12:28 UTC (permalink / raw)
  To: guile-user

On Wed, 22 Jul 2020 12:54:20 +0100
Chris Vine <vine35792468@gmail.com> wrote:
> On Wed, 22 Jul 2020 01:56:53 +0200
> Jan Wielkiewicz <tona_kosmicznego_smiecia@interia.pl> wrote:
> > Hello,
> > 
> > I started writing my project in Guile with GOOPS and fibers (I'll
> > release it once it stops being a shame and starts working!), but I
> > encountered a problem:
> > 
> > The example from fibers' manual doesn't work:
> > 
> > (lambda ()
> >  (spawn-fiber (lambda () (display "hey!\n")))))
> > 
> > It doesn't print "hey" as documented in the manual.
> > I use guile 3.0.4 and fibers 1.0.0 (from Guix).
> 
> (You are missing '(run-fibers' at the beginnng.  Presumably this was
> just a pasting error on your part.)
> 
> With that omission corrected, it used to work with guile-2.2.  Something
> seems to have changed with guile-3.0, because it only works for me with
> guile-3.0 if you set #:drain? to #t in the call to run-fibers.  Without
> that, run-fibers appears to return before init-thunk returns.
> 
> So it seems that there is either a bug in the manual or in the fibers
> implementation, probably the latter.

On further reflection I am not sure if it did used to work with
guile-2.2.  I think it may be a bug in the manual after all.  This works
OK:

  (display (run-fibers
            (lambda ()
              (let ((channel (make-channel)))
                (spawn-fiber
                 (lambda ()
                   (sleep 1) ;; do some work
                   (put-message channel "hello world")))
                (simple-format #t "~a~%" (get-message channel))
                "finished\n"))))

The point about:

  (run-fibers
   (lambda () (spawn-fiber
               (lambda ()
                 (set! v "Set")
                 (display "hey!\n")))))

is that spawn-fiber, and so init-thunk, will return straigntaway with a
new fiber object.

So I suspect setting #:drain? to #t will resolve your problem.

It might be worth reporting the bug in the manual as an issue on the
github repository (assuming the above is correct).



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: GOOPS and fibers - need help understanding what's wrong, bug in fibers/guile?
  2020-07-22 12:28   ` Chris Vine
@ 2020-07-24 16:19     ` Jan Wielkiewicz
  0 siblings, 0 replies; 4+ messages in thread
From: Jan Wielkiewicz @ 2020-07-24 16:19 UTC (permalink / raw)
  To: Chris Vine; +Cc: guile-user

Hello,

Dnia 2020-07-22, o godz. 13:28:14
Chris Vine <vine35792468@gmail.com> napisał(a):

> On further reflection I am not sure if it did used to work with
> guile-2.2.  I think it may be a bug in the manual after all.  This
> works OK:
> 
>   (display (run-fibers
>             (lambda ()
>               (let ((channel (make-channel)))
>                 (spawn-fiber
>                  (lambda ()
>                    (sleep 1) ;; do some work
>                    (put-message channel "hello world")))
>                 (simple-format #t "~a~%" (get-message channel))
>                 "finished\n"))))
> 
This one just worked, I don't know why, even without "display".
Something is buggy it seems.

> The point about:
> 
>   (run-fibers
>    (lambda () (spawn-fiber
>                (lambda ()
>                  (set! v "Set")
>                  (display "hey!\n")))))
> 
> is that spawn-fiber, and so init-thunk, will return straigntaway with
> a new fiber object.
> 
> So I suspect setting #:drain? to #t will resolve your problem.
#:drain solved the problem and I also managed to make my code work.

> It might be worth reporting the bug in the manual as an issue on the
> github repository (assuming the above is correct).
> 
Yes, I'll report it.

Thanks for helping me!

Posting my working code, so someone wanting to mix GOOPS with fibers
will have a working example:

(define-module (blocks block)
  #:use-module (fibers)
  #:use-module (fibers channels)
  #:use-module (oop goops)
  #:use-module (ice-9 match)
  #:use-module (srfi srfi-9) ;; records
  #:export (<peer>
            connect-peers
            send-message
            handle-message
            main
            p1
            p2
            get-buffer))

(define-class <peer> ()
  (input #:init-form '() #:getter get-input
         #:setter set-input #:init-keyword #:input)
  (output #:init-form '() #:getter get-output
          #:setter set-output #:init-keyword #:output)
  (buffer #:init-value '() #:setter set-buffer #:getter get-buffer))

;; Only connection, no messaging started.
(define-method (connect-peers (p1 <peer>) (p2 <peer>))
  (let ((p1-p2 (make-channel)) ;; p1 to p2
        (p2-p1 (make-channel))) ;; p2 to p1
    (set-input p1 p2-p1)
    (set-output p1 p1-p2)
    (set-input p2 p1-p2)
    (set-output p2 p2-p1)))

(define-method (send-message (p <peer>) msg)
  (spawn-fiber
   (lambda ()
     (put-message (get-output p) msg))))

(define-method (handle-message (p <peer>))
  (spawn-fiber
   (lambda ()
     (let loop ()
       (define msg (get-message (get-input p)))
       (match (pk msg)
              ('ping (send-message p 'pong))
              ('pong (send-message p 'ping)))
       (loop)))))

(define (main)
  (define p1 (make <peer>))
  (define p2 (make <peer>))
  (connect-peers p1 p2)
  (run-fibers
   (lambda ()
     (handle-message p1)
     (handle-message p2)
     (send-message p1 'ping))
   #:drain? #t))

(main)


---

Jan Wielkiewicz



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2020-07-24 16:19 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-07-21 23:56 GOOPS and fibers - need help understanding what's wrong, bug in fibers/guile? Jan Wielkiewicz
2020-07-22 11:54 ` Chris Vine
2020-07-22 12:28   ` Chris Vine
2020-07-24 16:19     ` Jan Wielkiewicz

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).