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
9
votes
4 answers

How to stop OCaml garbage collecting my reactive event handler?

I'm trying to use the OBus library with Lwt_react. This uses "functional reactive programming" for properties and signals. The problem (as noted in the React documentation) is that OCaml may garbage collect your callback while you're still using it.…
Thomas Leonard
  • 7,068
  • 2
  • 36
  • 40
7
votes
2 answers

Lwt leaking file descriptors, not sure if bug or my code

(Cross posted to lwt github issues) I have boiled down my usage to this code sample which will leak file descriptors. say you have: #require "lwt.unix" open Lwt.Infix let echo ic oc = Lwt_io.(write_chars oc (read_chars ic)) let program = let…
user1971598
7
votes
3 answers

Are streams in ocaml really used?

Looking at different ocaml projects, I don't see the built-in Streams in the language ever used. Even in the recent Real World Ocaml book, Streams are not mentioned at all, which is odd. What's the reason for that? Is it because Lwt or Core…
Sergi Mansilla
  • 12,495
  • 10
  • 39
  • 48
4
votes
0 answers

Integrating GTK and Lwt

I am working on a program in which I would like to use lablgtk and lwt. I have functions fetching data on lwt threads, then I would like to display the data in a GUI using lablgtk. I am struggling with the integration of lablgtk in the lwt…
Thomas
  • 347
  • 3
  • 19
4
votes
1 answer

How to make something lwt supported?

I am trying to understand the term lwt supported. So assume I have a piece of code which connect a database and write some data: Db.write conn data. It has nothing to do with lwt yet and each write will cost 10 sec. Now, I would like to use lwt.…
Jackson Tale
  • 25,428
  • 34
  • 149
  • 271
3
votes
2 answers

Does Lwt utilise data dependencies to increase paralleism

I'm trying to work out what specifically lwt is doing in a couple of examples: If I have: let%lwt x = f () in let%lwt y = g () in return () Does this run f then g, or since y doesn't rely on x will it run both in parallel?
Cjen1
  • 1,826
  • 3
  • 17
  • 47
3
votes
0 answers

^C while process is doing an Lwt read

I'm working on a little program using Lwt_io.read_into_exactly and other functions from Lwt_io. It all works great, but ^C (sigint) doesn't do anything, which forces me to kill -9 everytime I'm testing it, it's a bit annoying. Is there something I…
Ulrar
  • 895
  • 8
  • 17
3
votes
1 answer

Cstruct and Lwt_bytes

As far as I can tell, Lwt_bytes seems to use the same type as Cstruct (or probably uses cstruct itself), but some some reason I can't make the two of them work together : Lwt_io.write_from_exactly out b.Cstruct.buffer 0 16 Error: This expression…
Ulrar
  • 895
  • 8
  • 17
3
votes
2 answers

What's the proper usage for Lwt_io.read_int?

How do you properly use Lwt_io.read_int? I tried what I thought was the obvious usage but I don't get obvious results... open Lwt.Infix let _ = Lwt_main.run ( Lwt_io.print "Enter number: " >>= fun () -> Lwt_io.read_int…
G4143
  • 2,624
  • 4
  • 18
  • 26
3
votes
1 answer

Ocaml lwt never ending loop

I'm trying to write a terminal application with Lwt. Basically as longs as my app is running, I need to watch the terminal for input with Lwt_io.read_line. Is there a better way than the following (pseudocode) to achieve some kind of loop while my…
Seneca
  • 2,392
  • 2
  • 18
  • 33
3
votes
1 answer

LWT simple interaction with a subprocess

Here is a simple program that uses the Unix module to interact with a subprocess. I just launch a cat shell command, send it a string and read it back: #load "unix.cma";; (* Needed if you are in the toplevel *) let () = let sin, sout, serr =…
Kostya
  • 1,072
  • 11
  • 24
3
votes
1 answer

What are the differences between Lwt.async and Lwt_main.run on OCaml/Node.JS?

I am experimenting with js_of_ocaml and node.js. As you know, node.js makes extensive use of callbacks to implement asynchronous requests without introducing explicit threads. In OCaml we have a very nice threading library, Lwt, coming with a very…
Michaël Le Barbier
  • 6,103
  • 5
  • 28
  • 57
3
votes
1 answer

OCaml writing a timeout function using Async

I'm trying to write a function that tries to evaluate a function, but stops after a specific timeout. I tried to use Deferred.any, which returns a deferred that is fulfilled when one of the underlying deferred is fulfilled. type 'a output = OK of…
3
votes
1 answer

OCaml websocket "Invalid UTF8 data"

I am trying to build a loop with Lwt that will push a frame to a Websocket, wait for the response, print it to the screen, wait 60 seconds and then repeat the process again. I have been able to get something that compiles but I do not have it 100%…
Thomas
  • 347
  • 3
  • 19
3
votes
1 answer

Failed compiling ocaml-websocket

Trying to build ocaml-websocket, it fails with: File "lib/websocket.ml", line 202, characters 29-42: Error: The function applied to this argument has type ?buffer_size:int -> (Lwt_io.input_channel * Lwt_io.output_channel)…
Igor Liner
  • 97
  • 1
  • 5
1
2 3 4