From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: Zelphir Kaltstahl Newsgroups: gmane.lisp.guile.user Subject: Re: Guile fibers return values Date: Sun, 5 Jan 2020 02:30:06 +0100 Message-ID: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="178779"; mail-complaints-to="usenet@blaine.gmane.org" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Icedove/52.9.1 To: guile-user@gnu.org Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Sun Jan 05 02:30:41 2020 Return-path: Envelope-to: guile-user@m.gmane.org Original-Received: from lists.gnu.org ([209.51.188.17]) by blaine.gmane.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89) (envelope-from ) id 1inukZ-000SMo-3O for guile-user@m.gmane.org; Sun, 05 Jan 2020 02:30:39 +0100 Original-Received: from localhost ([::1]:38244 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1inukR-00082N-Mg for guile-user@m.gmane.org; Sat, 04 Jan 2020 20:30:31 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:52912) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1inuk7-00082B-Ru for guile-user@gnu.org; Sat, 04 Jan 2020 20:30:13 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1inuk6-0003EH-An for guile-user@gnu.org; Sat, 04 Jan 2020 20:30:11 -0500 Original-Received: from mout01.posteo.de ([185.67.36.65]:33878) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1inuk5-00039T-No for guile-user@gnu.org; Sat, 04 Jan 2020 20:30:10 -0500 Original-Received: from submission (posteo.de [89.146.220.130]) by mout01.posteo.de (Postfix) with ESMTPS id E4D3E16005F for ; Sun, 5 Jan 2020 02:30:07 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.de; s=2017; t=1578187807; bh=SszQfpUS3+nfsnPQxGL1MN0aVFm6S9nryenzbqUT/Zg=; h=To:From:Subject:Date:From; b=nltOacHreg/k5o5j4zIxQdq1b5964t1vzBQ3N7EJkzOSFL+TF9vyAeiFXiTkVbBwU v0oRaSdVBFpHTgVcx22CaqeSSrBUwi+2OXb5XTlOXRs5u+/KJTlfsbg0pITPeKls8W WSpSEAZ24+KxOaIbXGCCr9E9cP6xt2VEoMHgqY/vwyG0MyznV2zoNRxhbTMMNNqEut gd9hj7Hu8YkJVB+/L4ebEgnsf2IdOXPP4NcxQ8KqWC5rC6IlXUk5Cp8YYo2cdRSA37 gu8UkUXTkTRBsHiXbSKEnOUBfudu0+XpfelsSiLLFbZgdzTJ+PFxzwk/I2xaXLNLSP Mitex3RoLoN8Q== Original-Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 47r1KW1BWJz6tm5 for ; Sun, 5 Jan 2020 02:30:06 +0100 (CET) Content-Language: en-US X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 185.67.36.65 X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.23 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" Xref: news.gmane.org gmane.lisp.guile.user:15998 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