]> code.delx.au - gnu-emacs-elpa/blob - async.el
Added support for signal propagation
[gnu-emacs-elpa] / async.el
1 ;;; async --- Asynchronous processing in Emacs
2
3 ;; Copyright (C) 2012 John Wiegley
4
5 ;; Author: John Wiegley <jwiegley@gmail.com>
6 ;; Created: 18 Jun 2012
7 ;; Version: 1.0
8 ;; Keywords: async
9 ;; X-URL: https://github.com/jwiegley/async
10
11 ;; This program is free software; you can redistribute it and/or
12 ;; modify it under the terms of the GNU General Public License as
13 ;; published by the Free Software Foundation; either version 2, or (at
14 ;; your option) any later version.
15
16 ;; This program is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ;; General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25 \f
26 ;;; Commentary:
27
28 ;; Adds the ability to process Lisp concurrently, with a very simple syntax:
29 ;;
30 ;; (async-start
31 ;; ;; What to do in the child process
32 ;; (lambda ()
33 ;; (message "This is a test")
34 ;; (sleep-for 3)
35 ;; 222)
36 ;;
37 ;; ;; What to do when it finishes
38 ;; (lambda (result)
39 ;; (message "Async process done, result should be 222: %s" result)))
40 ;;
41 ;; If you omit the callback function, `async-start' will return a process
42 ;; object that you can call `async-get' on when you're ready to wait for the
43 ;; result value:
44 ;;
45 ;; (let ((proc (async-start
46 ;; ;; What to do in the child process
47 ;; (lambda ()
48 ;; (message "This is a test")
49 ;; (sleep-for 3)
50 ;; 222))))
51 ;; (message "I'm going to do some work here")
52 ;; ;; ....
53 ;; (message "Async process done, result should be 222: %s"
54 ;; (async-get proc)))
55 ;;
56 ;; If you don't want to use a callback, and you don't care about any return
57 ;; value form the child proces, pass the `ignore' symbol as the second
58 ;; argument:
59 ;;
60 ;; (async-start
61 ;; (lambda ()
62 ;; (delete-file "a remote file on a slow link" nil))
63 ;; 'ignore)
64 \f
65 ;;; Code:
66
67 (defgroup async nil
68 "Simple asynchronous processing in Emacs"
69 :group 'emacs)
70
71 (defvar async-callback)
72 (defvar async-callback-value nil)
73 (defvar async-callback-value-set nil)
74
75 (defun async-when-done (proc &optional change)
76 "Process sentinal used to retrieve the value from the child process."
77 (when (eq 'exit (process-status proc))
78 (with-current-buffer (process-buffer proc)
79 (if (= 0 (process-exit-status proc))
80 (progn
81 (goto-char (point-max))
82 (backward-sexp)
83 (let ((result (read (current-buffer))))
84 (if (and (listp result)
85 (eq 'async-signal (car result)))
86 (if (eq 'error (car (cdr result)))
87 (error (cadr (cdr result)))
88 (apply #'signal (cdr result)))
89 (if async-callback
90 (prog1
91 (funcall async-callback result)
92 (kill-buffer (current-buffer)))
93 (set (make-local-variable 'async-callback-value) result)
94 (set (make-local-variable 'async-callback-value-set) t)))))
95 (set (make-local-variable 'async-callback-value) 'error)
96 (set (make-local-variable 'async-callback-value-set) t)
97 (error "Async Emacs process failed with exit code %d"
98 (process-exit-status proc))))))
99
100 (defun async-batch-invoke ()
101 "Called from the child Emacs process' command-line."
102 (condition-case err
103 (prin1 (funcall (eval (read (nth 5 command-line-args)))))
104 (signal
105 (prin1 `(async-signal . ,err)))
106 (error
107 (prin1 `(async-signal . ,err)))))
108
109 (defun async-get (proc)
110 "Wait until PROC has successfully completed."
111 (with-current-buffer (process-buffer proc)
112 (while (and (not (eq 'exit (process-status proc)))
113 (not async-callback-value-set))
114 (sit-for 0 50))
115 (prog1
116 async-callback-value
117 (kill-buffer (current-buffer)))))
118
119 (defmacro async-start (start-func &optional finish-func)
120 "Fork execution of `start-func' into its own Emacs process.
121 `start-func' must be a `read'-able symbol or lambda form. It
122 cannot be a byte-compiled lambda.
123
124 `finish-func' is called with the result of `start-func' when that
125 process has completed. If it is nil, `async-start' will return a
126 process object that you can block on with `async-future-get' in
127 order to wait for the result of `start-func'. This would allow
128 you to start some expensive background processing at the
129 beginning of a command, then wait for the result only when you're
130 ready to use it."
131 (let ((bufvar (make-symbol "buf"))
132 (procvar (make-symbol "proc")))
133 (require 'find-func)
134 `(let* ((,bufvar (generate-new-buffer "*emacs*"))
135 (,procvar
136 (start-process "emacs" ,bufvar
137 (expand-file-name invocation-name
138 invocation-directory)
139 "-Q" "-l" (find-library-name "async")
140 "-batch" "-f" "async-batch-invoke"
141 (prin1-to-string (list 'quote ,start-func)))))
142 (with-current-buffer ,bufvar
143 (set (make-local-variable 'async-callback) ,finish-func)
144 (set-process-sentinel ,procvar #'async-when-done)
145 ,procvar))))
146
147 (defun async-test-1 ()
148 (interactive)
149 (message "Starting async-test-1...")
150 (async-start
151 ;; What to do in the child process
152 (lambda ()
153 (message "This is a test")
154 (sleep-for 3)
155 222)
156
157 ;; What to do when it finishes
158 (lambda (result)
159 (message "Async process done, result should be 222: %s" result)))
160 (message "Starting async-test-1...done"))
161
162 (defun async-test-2 ()
163 (interactive)
164 (message "Starting async-test-2...")
165 (let ((proc (async-start
166 ;; What to do in the child process
167 (lambda ()
168 (message "This is a test")
169 (sleep-for 3)
170 222))))
171 (message "I'm going to do some work here")
172 ;; ....
173 (message "Async process done, result should be 222: %s"
174 (async-get proc))))
175
176 (defun async-test-3 ()
177 (interactive)
178 (message "Starting async-test-3...")
179 (async-start
180 ;; What to do in the child process
181 (lambda ()
182 (message "This is a test")
183 (sleep-for 3)
184 (error "Error in child process")
185 222)
186
187 ;; What to do when it finishes
188 (lambda (result)
189 (message "Async process done, result should be 222: %s" result)))
190 (message "Starting async-test-1...done"))
191
192 (provide 'async)
193
194 ;;; async.el ends here