From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Manuel Giraud Newsgroups: gmane.emacs.help Subject: Fun with async processes Date: Mon, 31 Jan 2022 16:44:36 +0100 Message-ID: <87leyvyknv.fsf@elite.giraud> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="5434"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/29.0.50 (berkeley-unix) To: help-gnu-emacs Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Mon Jan 31 16:48:34 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 1nEYuv-0001AV-Kr for geh-help-gnu-emacs@m.gmane-mx.org; Mon, 31 Jan 2022 16:48:33 +0100 Original-Received: from localhost ([::1]:36690 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nEYuu-0000EC-6r for geh-help-gnu-emacs@m.gmane-mx.org; Mon, 31 Jan 2022 10:48:32 -0500 Original-Received: from eggs.gnu.org ([209.51.188.92]:53098) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nEYrS-0000B1-Jw for help-gnu-emacs@gnu.org; Mon, 31 Jan 2022 10:44:58 -0500 Original-Received: from ledu-giraud.fr ([51.159.28.247]:17778) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nEYrB-0001i7-A9 for help-gnu-emacs@gnu.org; Mon, 31 Jan 2022 10:44:43 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; s=rsa; bh=LvoPQZ4HdEC4b/gm /NiHzJ/9Hwvp+c0RMAGp6cfbweE=; h=date:subject:to:from; d=ledu-giraud.fr; b=nveVGyJpNIigmYRmv4435XKUB6LOhl6ETBezyWaxieO8VLFIYK AtqtVhsw+VtTSml+XYEOnvKp5ustUddS5zZDaEeKRt5hMoq+TcbCzoQWd+K4QpaPeO46pS D2xeGepoRc/E5LREPnwg2kQre2NLZ/Xzq29ksCtvTiB386VVKhjGQK9tuUU5oogEudaKuo 0M4INd60XZmwMbTLOGn/DwSgpdb5Wo4ywNF/dqwyYiEVOiGQiraDmmOVcmX1PfMtf3LhS/ 4eBbdqFGRklSmCU8TvLAsxcXw+j6e+TNxW11GVgPbdetgHJJI7VGTgrhyQvLxIck4aX1ZD 72C2gKv78Rag== Original-Received: from elite.giraud ( [10.1.1.1]) by ledu-giraud.fr (OpenSMTPD) with ESMTPSA id b5a09e5c (TLSv1.3:AEAD-AES256-GCM-SHA384:256:NO) for ; Mon, 31 Jan 2022 16:44:37 +0100 (CET) Received-SPF: pass client-ip=51.159.28.247; envelope-from=manuel@ledu-giraud.fr; helo=ledu-giraud.fr 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_PASS=-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:135612 Archived-At: Hi, Imagine, you have some async emacs processes with a sentinel each. What is the common emacs lisp pattern to have some kind of sentinel for all those processes (ie. do something when they all have finished)? I'd like to avoid a busy waiting like in the following example: --8<---------------cut here---------------start------------->8--- (defun chatty-sentinel (process event) (when (string-match "finished" event) (message "Chatty has finished talking.") (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 #'chatty-sentinel) process))) (defun thinker-sentinel (process event) (when (string-match "finished" event) (message "Thinker has finished thinking.") (kill-buffer (process-buffer 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 #'thinker-sentinel) process))) (defun myrun () (interactive) (let ((processes (list (chatty) (thinker)))) (while (cl-loop for process in processes thereis (eq (process-status process) 'run)) (sit-for 0.1)) (message "Both have finished."))) --8<---------------cut here---------------end--------------->8--- Cheers, -- Manuel Giraud