Why Async?

A hybrid approach to asynchronous programming

By using non-preemptive user-level threads with first-class blocking operations, Async avoids the performance compromises and synchronization woes of preemptive threads, without producing the confusing inversion of control that usually comes with event-driven systems.

High-level APIs and syntactic sugar simplify asynchronous code

When used with our ppx_let syntax extension, Async’s monadic interface means non-blocking code can be just as clean and readable as synchronous code.

First-class blocking operations that are expressed in the type system

OCaml’s type system knows about Async’s primitives, which means you can be confident about which code blocks, and which doesn’t, at compile-time. Helpful type errors make it clear when you are mixing asynchronous and synchronous code.

(* A non-blocking echo server *)
open Core
open Async

let run () =
  let%bind server =
      (Tcp.Where_to_listen.of_port 8765)
      (fun _addr r w ->
         Pipe.transfer ~f:Fn.id
           (Reader.pipe r) (Writer.pipe w))
  Tcp.Server.close_finished server

let () =
  Command.async ~summary:"An echo server"
    (Command.Param.return run)
  |> Command.run

Getting Started


$ opam install async

See here for async’s opam package file.


open Core
open Async


More docs, including detailed API docs, are available here.

Async comprises three packages, Async_kernel, Async_unix, and Async_extra.

  • Async_kernel contains Async’s core data structures, like Deferred. Async_kernel is portable, and so can be used in Javascript using Async_js. In principle it could also be used on Windows, but no scheduler has been written for Async on Windows as of yet.

  • Async_unix adds Unix dependencies for connections and the scheduler (and manages e.g., hooking into epoll).

  • Async_extra includes extras like command-line handling and RPC support built on top of those two packages.

Learn More

Real World OCaml chapter on Asynchronous Programming

Async is discussed in depth in Chapter 18 of Real World OCaml.

API Docs

Async’s API is broken into three packages, Async_kernel, Async_unix, and Async_extra, all documented extensively here.

A class on Async

Cornell’s CS department did a recitation on programming with Async that you can read here.