help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Fun with async processes


From: Manuel Giraud
Subject: Re: Fun with async processes
Date: Tue, 01 Feb 2022 14:30:11 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/29.0.50 (berkeley-unix)

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 via email to

[Prev in Thread] Current Thread [Next in Thread]