The following lisp code demonstrates how the value of a variable var
can be injected into the lambda used as START-FUNC
in (async-start START-FUNC &optional FINISH-FUNC)
.
I changed the lambda START-FUNC
with the help of a back-quote expression such that it returns a string modified with the value of the variable. Since the return value of START-FUNC
is printed by FINISH-FUNC
it is easier to check that the injection is successful.
(let ((var "value-of-variable"))
(async-start
;; What to do in the child process
`(lambda ()
(sleep-for 3)
,(format "This is a %s" var))
;; What to do when it finishes
(lambda (result)
(message "Async process done, result should be 222: %s"
result))))
Note that (format "This is a %s" var)
is run, i.e., the string "This is a value-of-variable"
is constructed) before the lambda is used. Therefore, this is not some kind of interprocess-communication where thread safety might be an issue.
Note also that this is quite the intended way to inject variable values into async-start
since the doc string of async-inject-variables
gives an example that works in that way:
(async-inject-variables INCLUDE-REGEXP &optional PREDICATE EXCLUDE-REGEXP)
Return a ‘setq’ form that replicates part of the calling environment. It sets the value for every variable matching INCLUDE-REGEXP and also PREDICATE. It will not perform injection for any variable matching EXCLUDE-REGEXP (if present). It is intended to be used as follows:
(async-start
‘(lambda ()
(require ’smtpmail)
(with-temp-buffer
(insert ,(buffer-substring-no-properties (point-min) (point-max)))
;; Pass in the variable environment for smtpmail
,(async-inject-variables "\‘\(smtpmail\|\(user-\)?mail\)-")
(smtpmail-send-it)))
’ignore)
In spite of the fact that async.el
defines a function async-send
that is not used in that file I assume that the package is not really intended for interprocess communication.
The reason for that assumption:
- Only
async-start
andasync-start-process
are mentioned in the doc string of the package async-ready
returnst
for the process statesexit
andsignal
of the child process -- both leading to the termination of the child process -- andasync-wait
just waits forasync-ready
to become trueasync-get
usesasync-ready
- Emacs is invoked with command line option
--batch
byasync-start
. So Emacs exists when thePROGRAM
has finished.