Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
Asynchronous IO programming in OCaml
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Goswin von Brederlow <goswin-v-b@w...>
Subject: Re: [Caml-list] Asynchronous IO programming in OCaml
"Jon Harrop" <> writes:

> Is there a tutorial on using something like LWT for asynchronous programming
> in OCaml? I'm looking for an example like an echo server that handles
> clients concurrently without blocking threads, so it can handle thousands of
> clients without significant performance degradation.

Sorry, not a tutorial but maybe it gives you some pointers for research.

What blockage do you want to remove?

1) sockets

You can set sockets to non-blocking and then use to wait for
activity. But that becomes inefficient if you really want thousands of
clients. This is really nothing ocaml specific but the same old select
problem as in C.

The solution (in C) is to use epoll and I haven't seen an ocaml binding
for that yet. Anyone?

Short of that mixing threads and select is an option.

2) disk I/O

There are (under linux) 3 choices for async disk I/O:

a) glibc -> internally threads
b) librt -> internally threads
c) libaio -> needs O_DIRECT to work on files

I've written some bindings for libaio and that works nicely.

No idea about that for windows or mac.

3) computations

Here you hit a bit of a problem. Ocaml doesn't support multiple
cores. (there is a multicore impementation but not standard.) So the
best you can do is split computations into little chunks and do time
sharing. Or offload jobs to worker threads that are not written in

What I like here is using CPS (continuation passing style). The idea is
that every function gets passed an argument, the continuation, which is
to be called next. If you have used Scanf.scanf then it is a bit like
that. A function only returns when it blocks, in which case it stores a
continuation in some queue to be continued later.

A pure echo server can be done fine with just a select loop and any C
example is easily translated to ocaml. Maybe try that and then look at a
bigger problem and ask more specific questions.