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: Re: Fun with async processes Date: Tue, 01 Feb 2022 14:30:11 +0100 Message-ID: <87o83q7lzw.fsf@elite.giraud> References: <87leyvyknv.fsf@elite.giraud> <87y22vq85b.fsf@elite.giraud> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="9392"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/29.0.50 (berkeley-unix) Cc: help-gnu-emacs@gnu.org To: tomas@tuxteam.de Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Tue Feb 01 18:34:31 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 1nEx30-00027r-RD for geh-help-gnu-emacs@m.gmane-mx.org; Tue, 01 Feb 2022 18:34:30 +0100 Original-Received: from localhost ([::1]:33310 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nEx2z-0003DI-CF for geh-help-gnu-emacs@m.gmane-mx.org; Tue, 01 Feb 2022 12:34:29 -0500 Original-Received: from eggs.gnu.org ([209.51.188.92]:42828) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nEutt-0005bM-Rh for help-gnu-emacs@gnu.org; Tue, 01 Feb 2022 10:17:02 -0500 Original-Received: from ledu-giraud.fr ([51.159.28.247]:30960) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nEutr-0002Y0-ME for help-gnu-emacs@gnu.org; Tue, 01 Feb 2022 10:16:57 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; s=rsa; bh=dm/6uq942sa6/fyw iwxMxmAKc5dybGL0j++f96GZpz4=; h=in-reply-to:date:references:subject: cc:to:from; d=ledu-giraud.fr; b=v1HB0qhwvzbqxw6SPvayhzZtPsm9EPFdKWhHhR 7d0MKeLJUijuDRtS3q+m36+YjjQuzOfzCDRI04YrYrr2VfhYQlbEPJRgcJhk2r6eAf2bUE c+lWde5XCUHrTkZjHbqjJ+bUxDO1cw/pqox6j9t62t9FcgDhhEpNSpMqZtyeYnjSSnBhi7 evlW74vtc7YrQl+HQlUwhZ0rZIHdgogjOSaVLHfhnSQUaFKX2z4M5MdTZnPTxCoKe+7hKg 19mbuRQf0lxk0aUxVa2azKEx/i3o1xhOsG9CTVkebEdi7V8clIfWWuqprb3c94J5gUIZjD xSYEUfYZtmX6e3EE5SCgiLDA== Original-Received: from elite.giraud ( [10.1.1.1]) by ledu-giraud.fr (OpenSMTPD) with ESMTPSA id eb4bec41 (TLSv1.3:AEAD-AES256-GCM-SHA384:256:NO); Tue, 1 Feb 2022 14:30:12 +0100 (CET) In-Reply-To: (tomas@tuxteam.de's message of "Tue, 1 Feb 2022 11:50:14 +0100") 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:135629 Archived-At: tomas@tuxteam.de writes: > I haven't one handy, sorry. But to illustrate my proposal (caveat: > untested!), I'll munge your code in that direction (sorry for that): [...] Thanks for this!=E2=80=A6 but your code is cleaner yet it is still missing = the "global" sentinel that says that both (chatty) and (thinker) have finished. I've done this: --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)) (global-sentinel))) (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)) (global-sentinel))) (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))) (defvar count-processes 0) (defvar mtx (make-mutex)) (defun global-sentinel () (with-mutex mtx (decf count-processes)) (when (zerop count-processes) (message "Both have finished."))) (defun myrun () (interactive) (setq count-processes (length (list (chatty) (thinker))))) --8<---------------cut here---------------end--------------->8--- But I'd like to avoid the global defvar if possible. > Lots of fun :) Indeed :) --=20 Manuel Giraud