From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Amirouche Boubekki Newsgroups: gmane.lisp.guile.user Subject: Re: Potluck - thread safe event loop with await semantics Date: Tue, 01 Mar 2016 20:39:30 +0000 Message-ID: References: <20160216214512.42e6fd39@bother.homenet> <878u2dosxu.fsf@gnu.org> <20160222174056.2091d806@dell.homenet> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: multipart/alternative; boundary=001a11471f86bd35f7052d02c309 X-Trace: ger.gmane.org 1456864796 9064 80.91.229.3 (1 Mar 2016 20:39:56 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 1 Mar 2016 20:39:56 +0000 (UTC) To: Chris Vine , guile-user@gnu.org Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Tue Mar 01 21:39:56 2016 Return-path: Envelope-to: guile-user@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1aar5G-0006I2-Mi for guile-user@m.gmane.org; Tue, 01 Mar 2016 21:39:55 +0100 Original-Received: from localhost ([::1]:52183 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aar5F-0006q4-Vt for guile-user@m.gmane.org; Tue, 01 Mar 2016 15:39:53 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:51304) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aar56-0006pe-QV for guile-user@gnu.org; Tue, 01 Mar 2016 15:39:46 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aar55-00022w-DV for guile-user@gnu.org; Tue, 01 Mar 2016 15:39:44 -0500 Original-Received: from mail-lf0-x22b.google.com ([2a00:1450:4010:c07::22b]:35816) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aar55-00022h-0i for guile-user@gnu.org; Tue, 01 Mar 2016 15:39:43 -0500 Original-Received: by mail-lf0-x22b.google.com with SMTP id j186so47258062lfg.2 for ; Tue, 01 Mar 2016 12:39:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:references:in-reply-to:from:date:message-id:subject:to; bh=iNmKRaXQFwb40/uNyc021H4ILkQJvd6vHOiI4iGInHs=; b=cJQd/MZZ8aMHP0eaEySB4BPXAhdOIAMXcE/Pfw1BE5K7XTiNP34HT0NrOn+4m3KFIn wTmSsHgb2s2sCw7DTN4b3AYCtxm+RhJQKbZ0tWk2bucynqp5PM04cSfa6bGyJZOg3Sgt +rIy7rdvitMp2MMa3xkIBMPeQhBHX6WVhCmfa4wftQL8JMrJBZOwyP2KYJieB+6iGlBY YqNpMXG/U0ST4a5C/ShATY9M10BJZHFSSk873R1OogPRfHQJCyrINWxMzzdWdmjomnoL gNk3i4V1TitETWLrcP5TKDFDaY9xAAKg6Zjkp4fjuY6GSKbRAwb1CfsG6vIIDw9hjU/u c1Ow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to; bh=iNmKRaXQFwb40/uNyc021H4ILkQJvd6vHOiI4iGInHs=; b=RxTzNCu/8Pyu/i5MgO/dXhofL8ANHE/7g7hZ1NbuEbU6iS6DBbOKBtwlJk+K1mIutF rjkhIYrwuAXRYdYEguAr+FHsUISMXJBPy/3ukmXDnBKZdG45Z1xoHkeBJcWYYBI5p5lh 6jDjDk//3xdfhxTb4YFEsePp0GsT2PJaqnSGnJ+DXqm8stePG8eaKbXqUaiVmX/bqZZ6 LAQQqKgFcRiO39FRyD3mgRc2f/l0/AYAjnmRvlU2s1fsdWPbo8/SPIHpRzlRNQ4kMqAG 4obchckjXHv8Xv4C0w1ja9UHsXufdMk9IlsMi10UEoV3CCAQ7yo7W8AzPqUSxTzFkqtc NXNg== X-Gm-Message-State: AD7BkJKkoJ3JazFi9qmBkNPfZuKVliw/cWDHy9AexMP9cvnI+Ne0woj7+aCywkTbv5HWON0ap4Dult9aZJaDqQ== X-Received: by 10.25.25.84 with SMTP id 81mr8446204lfz.71.1456864780692; Tue, 01 Mar 2016 12:39:40 -0800 (PST) In-Reply-To: <20160222174056.2091d806@dell.homenet> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a00:1450:4010:c07::22b X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane.org@gnu.org Original-Sender: guile-user-bounces+guile-user=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.user:12454 Archived-At: --001a11471f86bd35f7052d02c309 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable H=C3=A9llo On Mon, Feb 22, 2016 at 6:47 PM Chris Vine wrote: > On Mon, 22 Feb 2016 13:01:01 +0100 > ludo@gnu.org (Ludovic Court=C3=A8s) wrote: > > Chris Vine 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=E2=80=99. > > 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") > #f)) > (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) > (resume) > #f)) > (display "About to make second wait\n") > (await) > (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 https://friendpaste.com/6Q8HZw1SMOhAVCyfY2dAG7 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! --001a11471f86bd35f7052d02c309 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
H=C3=A9llo

On Mon, Feb 22, 2016 at 6:47 PM Chris Vine <chris@cvine.freeserve.co.uk> wrote:
<= /div>
On Mon, 22 Feb 2016 13:01:01 +0100
ludo@gnu.org (Ludovic= Court=C3=A8s) wrote:
> Chris Vine <chris@cvine.freeserve.co.uk> skribis:
>
> > It features an a-sync procedure (in coroutines.scm) which can be<= br> > > used to provide await semantics on asynchronous code (so as to > > remedy inversion of control), and will work with callbacks for an= y
> > event loop, including the glib event loop wrapped by guile-gnome.=
> > More to the point, it also provides a thread safe event loop for<= br> > > 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.=C2=A0 This includes tasks running on= other
> > threads, for which there is a helper procedure
> > a-sync-run-task-in-thread.
>
> Interesting.=C2=A0 Have you tried to integrate it with one of the
> object-oriented event loops like in GLib?=C2=A0 (Back in the day I thi= nking
> about something like that to avoid the callback hell in Guile-Avahi.)<= br> >
> Thanks for the tasty dish!=C2=A0 :-)
>
> Ludo=E2=80=99.

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)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0;; launch asynchronous task
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(g-idle-add (lambda ()
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0(display "In first async callback\n")
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0(resume "Hello via async\n")
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0#f))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(display "About to make first wait\n= ")
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(display (string-append "Back in wai= table procedure, and the callback says: " (await)))

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

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

;;;;;;;;

It's very different from a= syncio's await. The kind of coroutine you use are more general than Pyt= hon's coroutine.

The code would be nicer (and look mo= re like asyncio) if it looked like this snippet https://friendpaste.com/6Q8HZw1SMOhAVCyfY2d= AG7

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!
<= /div>
--001a11471f86bd35f7052d02c309--