unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Manuel Giraud <manuel@ledu-giraud.fr>
To: tomas@tuxteam.de
Cc: help-gnu-emacs@gnu.org
Subject: Re: Fun with async processes
Date: Tue, 01 Feb 2022 14:30:11 +0100	[thread overview]
Message-ID: <87o83q7lzw.fsf@elite.giraud> (raw)
In-Reply-To: <YfkQZil0ZI36WHW8@tuxteam.de> (tomas@tuxteam.de's message of "Tue, 1 Feb 2022 11:50:14 +0100")

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!… 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 :)
-- 
Manuel Giraud



  reply	other threads:[~2022-02-01 13:30 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-31 15:44 Fun with async processes Manuel Giraud
2022-01-31 15:56 ` tomas
2022-01-31 17:11   ` Emanuel Berg via Users list for the GNU Emacs text editor
2022-01-31 17:49     ` tomas
2022-01-31 18:11       ` Emanuel Berg via Users list for the GNU Emacs text editor
2022-02-01  8:54   ` Manuel Giraud
2022-02-01 10:50     ` tomas
2022-02-01 13:30       ` Manuel Giraud [this message]
2022-02-01 13:43         ` tomas
2022-02-01 13:54           ` Manuel Giraud
2022-02-01 14:31             ` tomas
2022-02-01 23:10         ` Emanuel Berg via Users list for the GNU Emacs text editor

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87o83q7lzw.fsf@elite.giraud \
    --to=manuel@ledu-giraud.fr \
    --cc=help-gnu-emacs@gnu.org \
    --cc=tomas@tuxteam.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).