]> code.delx.au - gnu-emacs-elpa/blob - async.el
Added async-inject-environment
[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 (defmacro async-inject-environment
76 (include-regexp &optional predicate exclude-regexp)
77 "Inject a part of the parent environment into an async function."
78 `(setq
79 ,@(let (bindings)
80 (mapatoms
81 (lambda (sym)
82 (if (and (boundp sym)
83 (or (null include-regexp)
84 (string-match include-regexp (symbol-name sym)))
85 (not (string-match
86 (or exclude-regexp "-syntax-table\\'")
87 (symbol-name sym))))
88 (let ((value (symbol-value sym)))
89 (when (or (null predicate)
90 (funcall (or predicate
91 (lambda (sym)
92 (let ((value (symbol-value sym)))
93 (or (not (functionp value))
94 (symbolp value))))) sym))
95 (setq bindings (cons `(quote ,value)
96 bindings))
97 (setq bindings (cons sym bindings)))))))
98 bindings)))
99
100 (defun async-when-done (proc &optional change)
101 "Process sentinal used to retrieve the value from the child process."
102 (when (eq 'exit (process-status proc))
103 (with-current-buffer (process-buffer proc)
104 (if (= 0 (process-exit-status proc))
105 (progn
106 (goto-char (point-max))
107 (backward-sexp)
108 (let ((result (read (current-buffer))))
109 (if (and (listp result)
110 (eq 'async-signal (car result)))
111 (if (eq 'error (car (cdr result)))
112 (error (cadr (cdr result)))
113 (signal (cadr result)
114 (cddr result)))
115 (if async-callback
116 (prog1
117 (funcall async-callback result)
118 (kill-buffer (current-buffer)))
119 (set (make-local-variable 'async-callback-value) result)
120 (set (make-local-variable 'async-callback-value-set) t)))))
121 (set (make-local-variable 'async-callback-value) 'error)
122 (set (make-local-variable 'async-callback-value-set) t)
123 (error "Async Emacs process failed with exit code %d"
124 (process-exit-status proc))))))
125
126 (defun async-batch-invoke ()
127 "Called from the child Emacs process' command-line."
128 (condition-case err
129 (prin1 (funcall (eval (read (nth 5 command-line-args)))))
130 (signal
131 (prin1 `(async-signal . ,err)))
132 (error
133 (prin1 `(async-signal . ,err)))))
134
135 (defun async-get (proc)
136 "Wait until PROC has successfully completed."
137 (with-current-buffer (process-buffer proc)
138 (while (and (not (eq 'exit (process-status proc)))
139 (not async-callback-value-set))
140 (sit-for 0 50))
141 (prog1
142 async-callback-value
143 (kill-buffer (current-buffer)))))
144
145 (defmacro async-start (start-func &optional finish-func)
146 "Fork execution of `start-func' into its own Emacs process.
147 `start-func' must be a `read'-able symbol or lambda form. It
148 cannot be a byte-compiled lambda.
149
150 `finish-func' is called with the result of `start-func' when that
151 process has completed. If it is nil, `async-start' will return a
152 process object that you can block on with `async-future-get' in
153 order to wait for the result of `start-func'. This would allow
154 you to start some expensive background processing at the
155 beginning of a command, then wait for the result only when you're
156 ready to use it."
157 (let ((bufvar (make-symbol "buf"))
158 (procvar (make-symbol "proc")))
159 (require 'find-func)
160 `(let* ((,bufvar (generate-new-buffer "*emacs*"))
161 (,procvar
162 (start-process "emacs" ,bufvar
163 (expand-file-name invocation-name
164 invocation-directory)
165 "-Q" "-l" (find-library-name "async")
166 "-batch" "-f" "async-batch-invoke"
167 (prin1-to-string (list 'quote ,start-func)))))
168 (with-current-buffer ,bufvar
169 (set (make-local-variable 'async-callback) ,finish-func)
170 (set-process-sentinel ,procvar #'async-when-done)
171 ,procvar))))
172
173 (defun async-test-1 ()
174 (interactive)
175 (message "Starting async-test-1...")
176 (async-start
177 ;; What to do in the child process
178 (lambda ()
179 (message "This is a test")
180 (sleep-for 3)
181 222)
182
183 ;; What to do when it finishes
184 (lambda (result)
185 (message "Async process done, result should be 222: %s" result)))
186 (message "Starting async-test-1...done"))
187
188 (defun async-test-2 ()
189 (interactive)
190 (message "Starting async-test-2...")
191 (let ((proc (async-start
192 ;; What to do in the child process
193 (lambda ()
194 (message "This is a test")
195 (sleep-for 3)
196 222))))
197 (message "I'm going to do some work here")
198 ;; ....
199 (message "Async process done, result should be 222: %s"
200 (async-get proc))))
201
202 (defun async-test-3 ()
203 (interactive)
204 (message "Starting async-test-3...")
205 (async-start
206 ;; What to do in the child process
207 (lambda ()
208 (message "This is a test")
209 (sleep-for 3)
210 (error "Error in child process")
211 222)
212
213 ;; What to do when it finishes
214 (lambda (result)
215 (message "Async process done, result should be 222: %s" result)))
216 (message "Starting async-test-1...done"))
217
218 (provide 'async)
219
220 ;;; async.el ends here