[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Potluck - thread safe event loop with await semantics

From: Amirouche Boubekki
Subject: Re: Potluck - thread safe event loop with await semantics
Date: Tue, 01 Mar 2016 20:39:30 +0000


On Mon, Feb 22, 2016 at 6:47 PM Chris Vine <address@hidden> wrote:
On Mon, 22 Feb 2016 13:01:01 +0100
address@hidden (Ludovic Courtès) wrote:
> Chris Vine <address@hidden> skribis:
> > It features an a-sync procedure (in coroutines.scm) which can be
> > used to provide await semantics on asynchronous code (so as to
> > remedy inversion of control), and will work with callbacks for any
> > event loop, including the glib event loop wrapped by guile-gnome.
> > More to the point, it also provides a thread safe event loop for
> > guile (event-loop.scm) with support for watches on ports/file
> > descriptors, and now supports proper timeouts, and permits events
> > to be posted by other tasks.  This includes tasks running on other
> > threads, for which there is a helper procedure
> > a-sync-run-task-in-thread.
> Interesting.  Have you tried to integrate it with one of the
> object-oriented event loops like in GLib?  (Back in the day I thinking
> about something like that to avoid the callback hell in Guile-Avahi.)
> Thanks for the tasty dish!  :-)
> Ludo’.

This is an example of how you might use a-sync with guile-gnome:


(use-modules (gnome glib) (coroutines))

(define main-loop (g-main-loop-new #f #f))

(a-sync (lambda (await resume)
         ;; launch asynchronous task
         (g-idle-add (lambda ()
                       (display "In first async callback\n")
                       (resume "Hello via async\n")
         (display "About to make first wait\n")
         (display (string-append "Back in waitable procedure, and the callback says: " (await)))

         ;; launch another asynchronous task
         (g-idle-add (lambda ()
                       (display "In second async callback\n")
                       (g-main-loop-quit main-loop)
         (display "About to make second wait\n")
         (display "Quitting\n")))

(display "Starting main loop\n")
(g-main-loop-run main-loop)


It's very different from asyncio's await. The kind of coroutine you use are more general than Python's coroutine.

The code would be nicer (and look more like asyncio) if it looked like this snippet

Basically await procedure takes another procedure that implement the resume behavior. In a sens `(await idle)` is similar to some kind of `(await sleep 5)` implementation. Not sure this last sentence make really sens to you.

Happy hacking!

reply via email to

[Prev in Thread] Current Thread [Next in Thread]