libevent for Lisp: A Signal Example
Posted on October 19, 2012 by oubiwann

At MindPool, there are several async I/O options we've been exploring for Lisp:
- CMUCL/SBCL's SERV E-EVENT
- IOlib< /li>
- cl-event (libevent for Lisp, using cffi; three years old)
- cl-async (also using cffi libevent wrapper, actively developed)
I was ready to dive in and get things current, when one last Google search turned up cl-async. This little bugger was hard to find, as at that point it had not been listed on CLiki. (But it is now :-)). Andrew Lyon has done a tremendous amount of work on cl-async, with a very complete set of bindings for libevent. This is just what I had been looking for, so I jumped in immediately.
As one might imagine from the topic of this post, there's a lot to be explored, uncovered, and developed further around async programming in Lisp. I'll start off slowly with a small example, and add more over the course of time.
I also hope to cover IOlib and SBCL's SERVE-EVENT in some future posts. Time will tell... For now, let's get started with cl-async in SBCL :-)
Dependencies
In a previous post, I discussed getting an environment set up with SBCL, I the rest of this post assumes that has been read and done :-)
Getting cl-async and Setting Up an SBCL Environment for Hacking
Now let's download cl-async and install the Libevent bindings :-)
Note that the as: is a nickname for the package namespace cl-async:.
As one might expect, there is a function to start the event loop. However, what is a little different is that one doesn't initialize the event loop directly, but with a callback. As such, one cannot set up handlers, etc., except within the scope of this callback.
We've got the setup-handler function for that, which adds a callback for a SIGINT event. Let's try it out :-)
Once your script has finished loading the core, you should see output like the above, with no return to the shell prompt.
When we send a SIGINT with C, we can watch our callback get fired:
Next up, we'll take a look at other types of handlers in cl-async.
Comments?
This blog doesn't use standard (embedded) comments; however, since
the site is hosted on Github, if there is
something you'd like to share, please do so by
opening a
"comment" ticket!