reddit / emacs

Async with ability to interrupt Emacs?

avindroth · 2022-05-20 01:19
I figure this is already a hot topic, but I have been unable to find the right info. I have been looking into jwiegley’s async, and see that you can create futures that allow emacs to be blocked until its child process exits. Is there a way for Emacs to be instead interrupted when child process exits? Then Emacs doesn’t need to be blocked until the child process exits.
Ok-Promise-500 · 2022-05-20 02:50
Are you after process sentinels? Use it with `set-process-sentinel` to execute something on a finished event: (defun execute-on-exit (process event) (when (equal event "finished\n") ...)) (set-process-sentinel my-process #'execute-on-exit)
Kribbstar · 2022-05-20 05:27
You can either use: async-start START-FUNC FINISH-FUNC And pass a function to FINISH-FUNC and it will be run when START-FUNC is finished. Or you could run async-get FUTURE On a timer and check if the future is finished or not.
avindroth · 2022-05-20 20:15
Is there a way to access the return value? I am trying something like (set-process-sentinel (async-start (lambda () (sleep-for 3) “Done”)) (lambda (proc string) (message string)))
Ok-Promise-500 · 2022-05-21 01:41
Not that I know of for sentinels, I use them for a process outputting to a buffer or similar. Kribbstar's solution does what you want, though: (async-start (lambda () (sleep-for 3) "Done") (lambda (result) (message "%s" result)))
avindroth · 2022-05-21 04:19
I see, I must have had a wrong understanding of async-start. I wonder why the following doesn't work: (async-start (lambda () (sleep-for 1) 222) (lambda (res) (beginning-of-buffer))) Calling `(other-window 1)` instead of `(beginning-of-buffer)` works though. EDIT: Current buffer must be some other buffer for calling the process, as the following works. Setting saved-buffer with a let binding doesn't work though. (setq saved-buffer (current-buffer)) (async-let ((n (progn (sleep-for 1) 5))) (switch-to-buffer saved-buffer) (beginning-of-buffer) (next-line n))