Questions tagged [ocaml-lwt]

Use for questions regarding Lwt, an OCaml promise and concurrent programming library.

Lwt is 's concurrent programming library. It provides a single data type: the promise, which is a value that will become determined in the future. Creating a promise spawns a computation. When that computation is I/O, Lwt runs it in parallel with your OCaml code.

OCaml code, including creating and waiting on promises, is run in a single thread by default, so you don't have to worry about locking or preemption. You can detach code to be run in separate threads on an opt-in basis.

Useful resources

49 questions
2
votes
1 answer

Troubleshooting Compilation Error with lwt_ppx in OCaml: Unable to Compile File

I'm encountering an issue while using lwt_ppx and the let%lwt syntax as described in the CS3110 textbook's section on promises (https://cs3110.github.io/textbook/chapters/ds/promises.html). In the code snippet provided: open Lwt_io let p = …
abu fahad
  • 23
  • 3
2
votes
1 answer

Lwt.async() not working as expected

I'm developing a web service in Ocaml on top of MirageOS(Unix) and at the moment I'm having some trouble with Lwt.async(). The Lwt documentation states the following: val async : (unit -> 'a t) -> unit async f starts a thread without waiting for…
Vittorio Cozzolino
  • 931
  • 1
  • 14
  • 31
2
votes
1 answer

How to correctly start a process from a specific directory with Lwt

It is easy to start a process from a specific directory with Lwt using the functions Sys.getpwd, Lwt_unix.chdir and Lwt_process.exec: Use Sys.getpwd to save the current working directory Use Lwt_unix.chdir to change to the specific directory Use…
Michaël Le Barbier
  • 6,103
  • 5
  • 28
  • 57
2
votes
1 answer

How can I use a Lwt library from Async code?

I found this: https://github.com/janestreet/lwt-async But I don't understand at all where it's supposed to go. Is it a replacement for Lwt that I just have to drop in my folder and link at compile time? Is there a another way to call Lwt code from a…
SGr
  • 853
  • 2
  • 9
  • 15
1
vote
1 answer

Terminate an Lwt thread from another thread

Here I have a function which accept a TCP connection and run two Lwt threads handle_connection and send_message. Each time that a connection is terminated I am notified in the handle_connection thread, so I can terminate it loops, but then I want to…
1
vote
1 answer

Putting lwt.t code in infinite loop in Ocaml for mirage os

I have the following code block which I modified from the mirageOS github repo: open Lwt.Infix module Main (KV: Mirage_kv.RO) = struct let start kv = let read_from_file kv = KV.get kv (Mirage_kv.Key.v "secret") >|= function …
atakanyenel
  • 1,367
  • 1
  • 15
  • 20
1
vote
1 answer

How to send multiple TCP messages and continue when one has succeeded

I'm writing some networking code currently and I need to send out a large number of messages and then wait for a single response. Given that I have a function that returns the input and output channels for a socket I have: let resps = List.map uris…
Cjen1
  • 1,826
  • 3
  • 17
  • 47
1
vote
2 answers

Ocaml Lwt type confusion

I'm confused why the Lwt print function Lwt_io.print has type string -> unit Lwt.t But if I run Lwt_io.print "a" >>= fun () -> Lwt_io.print "b";; the result is that "ab" is printed and type unit is returned. I would imagine that this would be a…
Ryan Marr
  • 83
  • 7
1
vote
1 answer

Seting a ref to the result of a Lwt_io.read_line in a chain of bound threads

I'm creating a chat server, and I have a function that handles login. There exists a preset ref called nick and a preset input stream imp. My code is as follows: let handle_login nr (inp,outp) = Lwt_io.printl "" >>=…
Elsa
  • 43
  • 6
1
vote
1 answer

Creating GET/POST services Ocsigen

I am building a app using Ocsigen, this app will not be connected to a database and the goal is to copy the content from the main website to here. (i am using curl to do Get requests) So my problem here is, I am trying to do a "log in" with a user,…
Joao Saraiva
  • 91
  • 2
  • 15
1
vote
1 answer

Ocaml Lwt.wait()

I have a question about lwt's wait function and how I would use it in my own custom function which would return a 'a Lwt.t thread. First let's show you an example. open Lwt_io open Lwt.Infix let i, o = Lwt_io.pipe() let get_int () = let t, w =…
G4143
  • 2,624
  • 4
  • 18
  • 26
1
vote
1 answer

Ocaml lwt read stdout from other process

I'm trying to build a new frontend in Ocaml for a terminal based application. The main idea is the spawn a new process with Lwt: let cmd = shell "./otherterminalapp" in let p = open_process_full cmd; And then later write stuff to the process'…
Seneca
  • 2,392
  • 2
  • 18
  • 33
1
vote
1 answer

Lwt and Cohttp: `Fatal error: exception Unix.Unix_error(Unix.ECONNRESET, "read", "")`

I have simple HTTP server in Ocaml with Cohttp and Lwt. When I run wrk the application crashes around 50% of the time as soon as wrk finishes. I imagine the crash is triggered by the unexpected tear-down of the connection. I see the following error…
user6307701
1
vote
1 answer

Error: Unbound record field Server.callback - Ocaml

I am following a tutorial that explains how to make a simple web server in OCaml with lwt and Cohttp. I have a _tags file that contains the following: true: package(lwt), package(cohttp), package(cohttp.lwt) And a webserver.ml: open Lwt open…
David B.
  • 371
  • 5
  • 17
1
vote
1 answer

How to use Lwt with Labltk?

How to write a program using Lwt and interacting with a Tk interface. I tried to run the Tk main thread in a Lwt preemptive thread with let () = Lwt.async(Lwt_preemptive.detach Tkthread.thread_main); Lwt_main.run(main Tkthread.top) where main…
Michaël Le Barbier
  • 6,103
  • 5
  • 28
  • 57