[Top][All Lists]

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

Fun with async processes

From: Manuel Giraud
Subject: Fun with async processes
Date: Mon, 31 Jan 2022 16:44:36 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/29.0.50 (berkeley-unix)


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)

(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)

(defun myrun ()
  (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---

Manuel Giraud

reply via email to

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