From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: tomas@tuxteam.de Newsgroups: gmane.emacs.help Subject: Re: Fun with async processes Date: Tue, 1 Feb 2022 11:50:14 +0100 Message-ID: References: <87leyvyknv.fsf@elite.giraud> <87y22vq85b.fsf@elite.giraud> Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="k2zGZ+uompRn0UKY" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="36118"; mail-complaints-to="usenet@ciao.gmane.io" Cc: help-gnu-emacs@gnu.org To: Manuel Giraud Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Tue Feb 01 11:51:50 2022 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nEqlK-0009Bk-7u for geh-help-gnu-emacs@m.gmane-mx.org; Tue, 01 Feb 2022 11:51:50 +0100 Original-Received: from localhost ([::1]:57888 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nEqlI-0000Nh-RQ for geh-help-gnu-emacs@m.gmane-mx.org; Tue, 01 Feb 2022 05:51:48 -0500 Original-Received: from eggs.gnu.org ([209.51.188.92]:51634) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nEqjs-0000NV-R4 for help-gnu-emacs@gnu.org; Tue, 01 Feb 2022 05:50:20 -0500 Original-Received: from mail.tuxteam.de ([5.199.139.25]:51436) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nEqjp-00007B-RU for help-gnu-emacs@gnu.org; Tue, 01 Feb 2022 05:50:20 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tuxteam.de; s=mail; h=In-Reply-To:Content-Type:MIME-Version:References:Message-ID:Subject :Cc:To:From:Date:Sender:Reply-To:Content-Transfer-Encoding:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=FhyQU671PWvK4gy0S/rA4gRG2WXAgxuzijK+vkZra68=; b=J3FpTE2VM90JPe/xXEXTZOZFLY 7duwJ5Pyxrb13N8sV16C0rlov00us2EWFHDm5n7yenl5pr+VykcoY5wyN6UJaVl5ILmqgfnRjxaUU nCbYh2q/lzlELaXov5Ph37F8w4w6cl+3da5x0bKD0TGw6RhCaSmgW3wkj/g1J89YV9CIfg6Xofc3T AcKNUMGRzio2Cp2VbuoI/qkZA/F4kumulXG0G23y4993WLbN768Ed8ptMZteZjoi4BWnZaXZNYHek BDetZGZVg94HIpEfxtacw8gxfSROj8cCMqcGpuQratrds6pO2leYyA+UdCG8tQI612zCEkkPkOyzp OQv21ohQ==; Original-Received: from tomas by mail.tuxteam.de with local (Exim 4.94.2) (envelope-from ) id 1nEqjm-0002K9-UJ; Tue, 01 Feb 2022 11:50:15 +0100 Content-Disposition: inline In-Reply-To: <87y22vq85b.fsf@elite.giraud> Received-SPF: pass client-ip=5.199.139.25; envelope-from=tomas@tuxteam.de; helo=mail.tuxteam.de X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.io gmane.emacs.help:135621 Archived-At: --k2zGZ+uompRn0UKY Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Tue, Feb 01, 2022 at 09:54:24AM +0100, Manuel Giraud wrote: > writes: >=20 > > An obvious approach is to have each sentinel call a "global" sentinel > > which checks whether there are any processes still running (akin to your > > myrun, only that it just gets called after each process's > > termination). >=20 > So I have to have an external variable that lists all the processes? You have it already... > (defun myrun () > (interactive) > (let ((processes (list (chatty) (thinker)))) =2E.. here > > Alternatively (this is typically the approach I take) is to have one > > sentinel [...] > Do you have an example or somewhere to look at in emacs for this > approach? Thanks. I haven't one handy, sorry. But to illustrate my proposal (caveat: untested!), I'll munge your code in that direction (sorry for that): (defun my-sentinel (tag process event) (when (string-match "finished" event) (cond ((eq tag 'chatty) (message "Chatty has finished talking.")) ((eq tag 'thinker) (message "Thinker has finished thinking.")) (t (message "Process %S has finished. I don't know that guy" tag))) (kill-buffer (process-buffer process)))) (defun chatty () (let* ((buffer (generate-new-buffer "chatty")) (process (start-process-shell-command (buffer-name buffer) buffer "find ~/.emacs.d -type f"))) (when process (set-process-sentinel process (lambda (process event) (my-sentinel 'chatty process event)) process)))) (defun thinker () (let* ((buffer (generate-new-buffer "thinker")) (process (start-process-shell-command (buffer-name buffer) buffer "for i in $(jot 10); do (echo $i && sleep $i) done"))) (when process (set-process-sentinel process (lambda (process event) (my-sentinel 'thinker process event)) process)))) Embellishments always possible, of course. For example, instead of putting everywhere a (lambda (...) ...), you could build a function `make-sentinel' taking a tag and returning a sentinel; Also, instead of that `cond' which gets longer and longer, you could have a hash table dispatching to the tag-specific code; Your `make-sentinel' could populate that hash table. If you (or your application) prefer static code to this (admittedly highly) dynamic approach, you might consider translating all that to the macro realm. Lots of fun :) Cheers --=20 t --k2zGZ+uompRn0UKY Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iF0EABECAB0WIQRp53liolZD6iXhAoIFyCz1etHaRgUCYfkQXgAKCRAFyCz1etHa Rn7dAJ91WUFP3tFclQN3gL6aNR9YGC6VBACbBdf8t5Ycr06Hes3RORFAgbc8bOo= =w/sS -----END PGP SIGNATURE----- --k2zGZ+uompRn0UKY--