[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

async.el: A simple asynchronous framework for Emacs

From: John Wiegley
Subject: async.el: A simple asynchronous framework for Emacs
Date: Mon, 18 Jun 2012 21:51:31 -0500
User-agent: Gnus/5.130006 (Ma Gnus v0.6) Emacs/24.1 (darwin)


I think we've all wanted threading and asynchronicity in Emacs for some time
now (Gnus, anyone?), and there have been many attempts to provide it.  I
propose a module for inclusion in Emacs, async.el, which offers a very ease to
use model for asynchronicity, without the need for any threading (and its
attendant complexities).  It should work on every platform that supports
asynchronous processes using `start-process'.

The whole interface is two functions: `async-start' and `async-get' (of which
the latter is optional).  Here is the basic form of use:

    (async-start (lambda () ...)

This will execute the lambda (which must *not* be byte-compiled -- in other
words, don't use `function' or #') in a child Emacs asynchronously.  When it's
done, the return value is passed to `function-to-call-when-done' as an
argument.  (If you don't care about the return value, pass the `ignore' symbol
as the second argument).

If you pass no second argument, a future is returned.  You can later call
`async-get' on this future to obtain the value, blocking if necessary.

That's it.  All you need to do asynchronous computation within Emacs.

Since it's likely that you'll want the child Emacs to heavy lifting based on
the parent Emacs' configuration, you can use `async-inject-environment' to
pass variable definitions across the process boundary:

    (async-start (lambda ()
                   (require 'some-module)
                   (async-inject-environment "\\`some-module-")

The variable definitions from the module "some-module" will be passed into the

Using these facilities, here's all it takes to send e-mail asynchronously with

    (defun async-smtpmail-send-it ()
       `(lambda ()
          (require 'smtpmail)
            (insert ,(buffer-substring-no-properties (point-min) (point-max)))
            ;; Pass in the variable environment for smtpmail
            ,(async-inject-environment "\\`\\(smtpmail\\|\\(user-\\)?mail\\)-")

I've also written dired-async.el, which performs copies, moves and deletes
asynchronously.  It works great with Tramp.

The files are hosted on GitHub presently:


One thing I would love to do is to work with the authors of other modules --
such as one of my all-time favorites, Gnus -- to see how a facility like this
can help improve user experience.

Comments welcome,

p.s. This e-mail sent asynchronously with smtpmail.el :)

reply via email to

[Prev in Thread] Current Thread [Next in Thread]