Skip to main content
3 of 4
added 1845 characters in body
Tobias
  • 33.7k
  • 1
  • 38
  • 78

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 and async-start-process are mentioned in the doc string of the package
  • async-ready returns t for the process states exit and signal of the child process -- both leading to the termination of the child process -- and async-wait just waits for async-ready to become true
  • async-get uses async-ready
  • Emacs is invoked with command line option --batch by async-start. So Emacs exists when the PROGRAM has finished.
Tobias
  • 33.7k
  • 1
  • 38
  • 78