From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@sympa.inria.fr Delivered-To: caml-list@sympa.inria.fr Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by sympa.inria.fr (Postfix) with ESMTPS id 7D2817EE9C for ; Sun, 27 Nov 2016 18:41:06 +0100 (CET) Authentication-Results: mail3-smtp-sop.national.inria.fr; spf=None smtp.pra=koen.dekeyser@gmail.com; spf=Pass smtp.mailfrom=koen.dekeyser@gmail.com; spf=None smtp.helo=postmaster@mail-wj0-f175.google.com Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of koen.dekeyser@gmail.com) identity=pra; client-ip=209.85.210.175; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="koen.dekeyser@gmail.com"; x-sender="koen.dekeyser@gmail.com"; x-conformance=sidf_compatible Received-SPF: Pass (mail3-smtp-sop.national.inria.fr: domain of koen.dekeyser@gmail.com designates 209.85.210.175 as permitted sender) identity=mailfrom; client-ip=209.85.210.175; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="koen.dekeyser@gmail.com"; x-sender="koen.dekeyser@gmail.com"; x-conformance=sidf_compatible; x-record-type="v=spf1" Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of postmaster@mail-wj0-f175.google.com) identity=helo; client-ip=209.85.210.175; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="koen.dekeyser@gmail.com"; x-sender="postmaster@mail-wj0-f175.google.com"; x-conformance=sidf_compatible IronPort-PHdr: =?us-ascii?q?9a23=3AngrMGReMcPr57H40R8YoI3/DlGMj4u6mDksu8pMi?= =?us-ascii?q?zoh2WeGdxcuybB7h7PlgxGXEQZ/co6odzbGH6Oa7BSdZu8rJmUtBWaQEbwUCh8?= =?us-ascii?q?QSkl5oK+++Imq/EsTXaTcnFt9JTl5v8iLzG0FUHMHjew+a+SXqvnYdFRrlKAV6?= =?us-ascii?q?OPn+FJLMgMSrzeCy/IDYbxlViDanb75/KBu7oR/fu8QZjoduN7g9xxTUqXZUZu?= =?us-ascii?q?pawn9lK0iOlBjm/Mew+5Bj8yVUu/0/8sNLTLv3caclQ7FGFToqK2866tHluhnF?= =?us-ascii?q?VguP+2ATUn4KnRpSAgjK9w/1U5HsuSbnrOV92S2aPcrrTbAoXDmp8qlmRAP0hC?= =?us-ascii?q?oBKjU09nzchM5tg6JBuB+upRJ/zY7abo+WKfRwYL/ScMgASmZdRMtcTTBNDp++?= =?us-ascii?q?YoYJEuEPPfxYr474p1YWqBW+AhejBOXywTFGmHD9wKg03PohEAHdwgMgBc8FvX?= =?us-ascii?q?PXrNXyKqgSV/q5w7TWwjXAcfxX2Sz955bSch06rvGMWKh/ccvVyUU1CwzFiVCQ?= =?us-ascii?q?pJXjMjiI2OoNtG2b4PBhVeKpk2Mnsxt+oiOzxsctlIbFnIUVykrL9Sh/3Y07Js?= =?us-ascii?q?W4RVZlbdK4FJZcrSKXOotsTs88Xm1kpDw2xqAEtJO7eiUB1Y4pyATFa/OddoiF?= =?us-ascii?q?+hLjW/iVITd/nH9lfaiwhxe28US5xOz8VNW43E9EriZYkNTBtmoB1xPU6siARf?= =?us-ascii?q?t9+lmu1SyT2ADU7+FIOUE0lazFJJ492rM8iIYfvEDZEiL1mEj6lrGaelg49uSy?= =?us-ascii?q?9ujqYLXrqoeZN4BuiwH+Nqoumta4AeQ9KgUOXG+b9vq61LH54EL2XrZKgeM2k6?= =?us-ascii?q?nCrp/aP98WpqG8AwBP04Yj7wyzACuh0NQdhXUHNk5KeAqbj4j1PFHDOOz3Au26?= =?us-ascii?q?g1SoiTtrw/HGPqb9ApjWNXjCkLLhfa5n5EJGyQozy8pf55NOBb0bLvLzQBy5iN?= =?us-ascii?q?uNNBYhMwW5x67NAchxy4IYETaVCKuYK7/JmVCN7+MrZeKLYdlGliz6Lq0G4PPo?= =?us-ascii?q?xVM00XwQbLKkx9NDa3e1GLJqLkaUcGHhmP8OFG4Lukw1S+m82w7KaiJae3vnB/?= =?us-ascii?q?F03To8Eo/zVYo=3D?= X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: =?us-ascii?q?A0BrAACXGTtYhq/SVdFeHAEBBAEBCgEBF?= =?us-ascii?q?gEBAQMBAQEJAQEBgw0BAQEBAT06cwoGB6RZlnskhX0CgV8HQxABAQEBAQEBAQE?= =?us-ascii?q?BARIBAQEICwsJHTCCMxiCHgEBAwESEQQZARsdAQMBCwYDAgQHAzQCAiEBAREBB?= =?us-ascii?q?QEcBhMiiDABAw8ICY80j1KBMj8yi1CBbBgFAR+DDQWDTAoZJw1UgzYBAQEBAQE?= =?us-ascii?q?BAwEBAQEBAQEYAgYSiweCSIFrgxqCXQWaHzWGSIZog1aCQY1xiUACHYQSgkgTH?= =?us-ascii?q?oETNU6BBxGDFyCBaT00iHUBAQE?= X-IPAS-Result: =?us-ascii?q?A0BrAACXGTtYhq/SVdFeHAEBBAEBCgEBFgEBAQMBAQEJAQE?= =?us-ascii?q?Bgw0BAQEBAT06cwoGB6RZlnskhX0CgV8HQxABAQEBAQEBAQEBARIBAQEICwsJH?= =?us-ascii?q?TCCMxiCHgEBAwESEQQZARsdAQMBCwYDAgQHAzQCAiEBAREBBQEcBhMiiDABAw8?= =?us-ascii?q?ICY80j1KBMj8yi1CBbBgFAR+DDQWDTAoZJw1UgzYBAQEBAQEBAwEBAQEBAQEYA?= =?us-ascii?q?gYSiweCSIFrgxqCXQWaHzWGSIZog1aCQY1xiUACHYQSgkgTHoETNU6BBxGDFyC?= =?us-ascii?q?BaT00iHUBAQE?= X-IronPort-AV: E=Sophos;i="5.31,707,1473112800"; d="scan'208,217";a="201889820" Received: from mail-wj0-f175.google.com ([209.85.210.175]) by mail3-smtp-sop.national.inria.fr with ESMTP/TLS/AES128-GCM-SHA256; 27 Nov 2016 18:41:05 +0100 Received: by mail-wj0-f175.google.com with SMTP id qp4so97338917wjc.3 for ; Sun, 27 Nov 2016 09:41:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=19PUeDkHDGvOqbDDb0P6/UJAPuHLvimrsw7PzEXLcIk=; b=Ac+phclLRHnERZ6c2nF87S4O71W9pPhzju8ISs50R8EKUsfAbblJXXdnmViGc5/7jT GOiHTPIln2ZMIy5NYoQfVVd2HZ618GtDVC9BQ7wzsei6KYZxudg7BdV8Pq0fTiSsyYjj EosBOvtxKxkZNGuiZVPLBM7+84fd6Pv6xgbKKazxWFtdua3ueCUhMoUX/sNNatEmgwKd 2jo3/LHXJBTBA14cTqUB5cRDocWkwbZ2stiONqjpZlcF6AblksFJtJ7CSxZXmXMLM0g3 K3EhzViLydkF+BE8BT7BVqomWpYJ/bWuinodICOcAE0BUjhYtSl/cwMSs2PEaqbaOLqF g1mA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=19PUeDkHDGvOqbDDb0P6/UJAPuHLvimrsw7PzEXLcIk=; b=jYD/HvNDOm44rFfF9Hci8goivNi8o134JbBnlhCx4tTxFvjUGd4rD2HF5MjMpOe4fF QKZoWUfkovdKQraoyo40SIWbOVdN5Zbq3RE0GRIJS9JZfjoQwlWWkes4x+rR7KsYyXuZ vfL1tiGLPgrBknEyzoIcZJfrg+5/IDQ8Rz9C5zAdxVFPG/O7LpdtjQCMEtlYgyqTvv56 zGIG6NNqq3u63hlwzx7cOOBjIOb+rG224fOAuARgr5lXaa96H2NIOVPzp6e3D1cCQM8/ dmbDAZzE8yv8TZVvUSys546OIGQP5mXHdFOGJRiitY/Z3viUjflSsgZXraYTD85oG3pH jNVw== X-Gm-Message-State: AKaTC02Bt2nngjckd9zI5bS3zH88gqHvP7OtfXhmPU/OsrhPHfQLIg0Q/8IlJb4Ck9WOqnXynqtKzeQuauf44w== X-Received: by 10.194.107.97 with SMTP id hb1mr15124161wjb.134.1480268465010; Sun, 27 Nov 2016 09:41:05 -0800 (PST) MIME-Version: 1.0 Received: by 10.28.69.153 with HTTP; Sun, 27 Nov 2016 09:41:04 -0800 (PST) Received: by 10.28.69.153 with HTTP; Sun, 27 Nov 2016 09:41:04 -0800 (PST) In-Reply-To: References: From: Koen De Keyser Date: Sun, 27 Nov 2016 18:41:04 +0100 Message-ID: To: Raphael Bauduin Cc: caml-list@inria.fr Content-Type: multipart/alternative; boundary=e89a8fb1fe66077da205424bdc75 Subject: Re: [Caml-list] Beginner's question on Lwt usage --e89a8fb1fe66077da205424bdc75 Content-Type: text/plain; charset=UTF-8 You need to run your lwt code within Lwt_main.run This starts the lwt scheduler. Otherwise your application just terminates once it has created the lwt thread (which is a simple Ocaml value). Koen On Nov 27, 2016 18:30, "Raphael Bauduin" wrote: > Hi, > > After writing a small tcp server in ocaml, I wanted to make it work > asynchronously with Lwt. > I changed the function handling the accept on the socket to look like this: > > let rec accept_connection sock:Unix.file_descr = > let socket_thread = Lwt.return ( Unix.accept sock ) in > let _ = Lwt.bind socket_thread > ( fun (fd, caller) -> > ignore(Lwt_io.printf "accepted\n%!"); > (*let _ = set_nonblock fd in*) > Lwt.return (readall fd) >>= fun a -> > ignore(Lwt_io.printf "%s\n%!" a); > ignore(Lwt.async (fun () -> Lwt.return (close fd) ) ); > Lwt.return 0; > ); in > accept_connection sock > > The complete code is at http://pastie.org/10971189 . > > But it doesn't seem to work. The problems are: > - nothing is printed on stdout > - when I open 2 connections, the second can only be closed after the first > has been closed. > > In the mean time I've discovered Lwt_unix, but before looking at it, I'd > like to understand what's wrong in my current code. Can you spot errors in > the code? General advice is also welcome! > > Thanks > > Raphael > > > --e89a8fb1fe66077da205424bdc75 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
You need to run your lwt code within Lwt_main.run=C2=A0
This starts the lwt scheduler. = Otherwise your application just terminates once it has created the lwt thre= ad (which is a simple Ocaml value).=C2=A0

=
Koen

On Nov 27, 2016 18:30, "Raphael Bauduin" <rblists@gmail.com> wrote:
Hi,

After writing a small tcp server in ocaml, I wanted to = make it work asynchronously with Lwt.
I changed the function hand= ling the accept on the socket to look like this:

<= div>let rec accept_connection sock:Unix.file_descr =3D
=C2=A0 let= socket_thread =3D Lwt.return ( Unix.accept sock ) in
=C2=A0 let = _ =3D Lwt.bind socket_thread
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0( = fun (fd, caller) ->
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0ignore(Lwt_io.printf "accepted\n%!");
=C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(*let _ =3D set_nonblock fd in*)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0Lwt.return (readall f= d) >>=3D fun a ->
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0ignore(Lwt_io.printf "%s\n%!" a);
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0ignore(Lwt= .async (fun () -> Lwt.return (close fd) ) );
=C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0Lwt.return 0;
=C2=A0= =C2=A0 =C2=A0 =C2=A0 =C2=A0); in
=C2=A0 =C2=A0accept_connection = sock

The complete code is at=C2=A0http://pastie.org/10971189 .


--e89a8fb1fe66077da205424bdc75--