From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by yquem.inria.fr (Postfix) with ESMTP id E44C3BBAF for ; Sun, 24 Oct 2010 22:42:16 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AvICAEI2xEzZSMDqgmdsb2JhbAChXhUBAQsLCAcTAx+6HoVIBI1T X-IronPort-AV: E=Sophos;i="4.58,233,1286143200"; d="scan'208";a="75954046" Received: from fmmailgate03.web.de ([217.72.192.234]) by mail2-smtp-roc.national.inria.fr with ESMTP; 24 Oct 2010 22:42:16 +0200 Received: from smtp05.web.de ( [172.20.4.166]) by fmmailgate03.web.de (Postfix) with ESMTP id 1DD0416913493; Sun, 24 Oct 2010 22:42:16 +0200 (CEST) Received: from [78.43.204.177] (helo=frosties.localdomain) by smtp05.web.de with asmtp (TLSv1:AES256-SHA:256) (WEB.DE 4.110 #24) id 1PA7OR-0006VA-00; Sun, 24 Oct 2010 22:42:16 +0200 Received: from mrvn by frosties.localdomain with local (Exim 4.72) (envelope-from ) id 1PA7OR-0000li-HS; Sun, 24 Oct 2010 22:42:15 +0200 From: Goswin von Brederlow To: "Jon Harrop" Cc: Subject: Re: [Caml-list] Asynchronous IO programming in OCaml References: <044101cb7367$10f94b30$32ebe190$@com> Date: Sun, 24 Oct 2010 22:42:15 +0200 In-Reply-To: <044101cb7367$10f94b30$32ebe190$@com> (Jon Harrop's message of "Sun, 24 Oct 2010 11:34:40 +0100") Message-ID: <87aam39vbs.fsf@frosties.localdomain> User-Agent: Gnus/5.110009 (No Gnus v0.9) XEmacs/21.4.22 (linux, no MULE) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Sender: goswin-v-b@web.de X-Sender: goswin-v-b@web.de X-Provags-ID: V01U2FsdGVkX1/kVuKwNg3uXgWg4+Tcb2tkRV2LYr9GRbiLJfrj 4CY0Mvo3L1LCTDkxgzbvLl1TYv858hYUlskLkw3PEO4K0ILwO0 fSLhpd+j8= X-Spam: no; 0.00; ocaml:01 ocaml:01 pointers:01 sockets:01 sockets:01 non-blocking:01 async:01 bindings:01 computations:01 computations:01 chunks:01 scanf:01 scanf:01 translated:01 mfg:98 "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 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