caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Goswin von Brederlow <goswin-v-b@web.de>
To: "Jon Harrop" <jon@ffconsultancy.com>
Cc: <caml-list@yquem.inria.fr>
Subject: Re: [Caml-list] Asynchronous IO programming in OCaml
Date: Sun, 24 Oct 2010 22:42:15 +0200	[thread overview]
Message-ID: <87aam39vbs.fsf@frosties.localdomain> (raw)
In-Reply-To: <044101cb7367$10f94b30$32ebe190$@com> (Jon Harrop's message of "Sun, 24 Oct 2010 11:34:40 +0100")

"Jon Harrop" <jon@ffconsultancy.com> 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 Unix.select 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
ocaml.

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.

MfG
        Goswin


      parent reply	other threads:[~2010-10-24 20:42 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-10-24 10:34 Jon Harrop
2010-10-24 12:51 ` [Caml-list] " philippe
2010-10-24 12:52 ` Dario Teixeira
2010-10-24 16:33   ` oliver
2010-10-24 18:50     ` Dario Teixeira
2010-10-24 19:04       ` bluestorm
2010-10-24 20:02       ` oliver
2010-10-24 21:51     ` Michael Ekstrand
2010-10-24 16:17 ` Jake Donham
2010-10-24 20:54   ` Anil Madhavapeddy
2010-10-24 22:50     ` Jérémie Dimino
2010-10-25  3:42       ` Markus Mottl
2010-10-25  7:49         ` Richard Jones
2010-10-25  8:42       ` Goswin von Brederlow
2010-10-25 11:10         ` Jérémie Dimino
     [not found]           ` <AANLkTimP77PDEChW3Yt6uUy_qxYpj6EOZWQ_==id-LBC@mail.gmail.com>
     [not found]             ` <20101025143317.GB32282@aurora>
2010-10-25 15:34               ` Yaron Minsky
2010-10-25 17:26                 ` Jérémie Dimino
2010-10-27  9:33                   ` Goswin von Brederlow
2010-10-27 11:18                     ` Jérémie Dimino
2010-10-27 13:43                       ` Goswin von Brederlow
2010-10-27 15:30                         ` Jérémie Dimino
2010-10-28  9:00                           ` Goswin von Brederlow
2010-10-28  9:28                             ` Jérémie Dimino
2010-10-28 10:11                               ` Goswin von Brederlow
2010-10-25 15:58           ` DS
2010-10-24 20:42 ` Goswin von Brederlow [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87aam39vbs.fsf@frosties.localdomain \
    --to=goswin-v-b@web.de \
    --cc=caml-list@yquem.inria.fr \
    --cc=jon@ffconsultancy.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).