All Projects → ocsigen → Lwt

ocsigen / Lwt

Licence: mit
OCaml promises and concurrent I/O

Programming Languages

ocaml
1615 projects

Projects that are alternatives of or similar to Lwt

Future
🚀 R package: future: Unified Parallel and Distributed Processing in R for Everyone
Stars: ✭ 735 (+45.54%)
Mutual labels:  asynchronous, promises, futures
future.callr
🚀 R package future.callr: A Future API for Parallel Processing using 'callr'
Stars: ✭ 52 (-89.7%)
Mutual labels:  promises, asynchronous, futures
Mioco
[no longer maintained] Scalable, coroutine-based, fibers/green-threads for Rust. (aka MIO COroutines).
Stars: ✭ 125 (-75.25%)
Mutual labels:  asynchronous, fibers, io
Swiftcoroutine
Swift coroutines for iOS, macOS and Linux.
Stars: ✭ 690 (+36.63%)
Mutual labels:  asynchronous, promises, futures
Asynchronous
Implementation-agnostic asynchronous code
Stars: ✭ 13 (-97.43%)
Mutual labels:  asynchronous, promises, futures
Tomorrowland
Lightweight Promises for Swift & Obj-C
Stars: ✭ 106 (-79.01%)
Mutual labels:  asynchronous, promises, futures
Luv
Cross-platform asynchronous I/O and system calls
Stars: ✭ 203 (-59.8%)
Mutual labels:  asynchronous, events, io
pg async.rs
Asynchronous, HA (master-master) PostgreSQL driver on top of libpq.
Stars: ✭ 40 (-92.08%)
Mutual labels:  asynchronous, futures
hunt
A refined core library for D programming language. The module has concurrency / collections / event / io / logging / text / serialization and more.
Stars: ✭ 86 (-82.97%)
Mutual labels:  asynchronous, io
Asyncro
⛵️ Beautiful Array utilities for ESnext async/await ~
Stars: ✭ 487 (-3.56%)
Mutual labels:  asynchronous, promises
asynchronous
A D port of Python's asyncio library
Stars: ✭ 35 (-93.07%)
Mutual labels:  asynchronous, fibers
PromisedFuture
A Swift based Future/Promises framework to help writing asynchronous code in an elegant way
Stars: ✭ 75 (-85.15%)
Mutual labels:  asynchronous, futures
java-red
Effective Concurrency Modules for Java
Stars: ✭ 25 (-95.05%)
Mutual labels:  asynchronous, futures
swift-futures
Demand-driven asynchronous programming in Swift
Stars: ✭ 32 (-93.66%)
Mutual labels:  promises, futures
tsukuyomi
Asynchronous Web framework for Rust
Stars: ✭ 81 (-83.96%)
Mutual labels:  asynchronous, futures
Futures
Lightweight promises for iOS, macOS, tvOS, watchOS, and Linux
Stars: ✭ 59 (-88.32%)
Mutual labels:  promises, futures
SocketIOUnity
A Wrapper for socket.io-client-csharp to work with Unity.
Stars: ✭ 69 (-86.34%)
Mutual labels:  events, io
node-google-calendar
Simple node module that supports Google Calendar API
Stars: ✭ 76 (-84.95%)
Mutual labels:  promises, events
Zio
ZIO — A type-safe, composable library for async and concurrent programming in Scala
Stars: ✭ 3,167 (+527.13%)
Mutual labels:  asynchronous, promises
Circuits
circuits is a Lightweight Event driven and Asynchronous Application Framework for the Python Programming Language with a strong Component Architecture.
Stars: ✭ 256 (-49.31%)
Mutual labels:  asynchronous, events

Lwt    version Travis status AppVeyor status

Lwt is a concurrent programming library for OCaml. 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.

Here is a simplistic Lwt program which requests the Google front page, and fails if the request is not completed in five seconds:

open Lwt.Syntax

let () =
  let request =
    let* addresses = Lwt_unix.getaddrinfo "google.com" "80" [] in
    let google = Lwt_unix.((List.hd addresses).ai_addr) in

    Lwt_io.(with_connection google (fun (incoming, outgoing) ->
      let* () = write outgoing "GET / HTTP/1.1\r\n" in
      let* () = write outgoing "Connection: close\r\n\r\n" in
      let* response = read incoming in
      Lwt.return (Some response)))
  in

  let timeout =
    let* () = Lwt_unix.sleep 5. in
    Lwt.return None
  in

  match Lwt_main.run (Lwt.pick [request; timeout]) with
  | Some response -> print_string response
  | None -> prerr_endline "Request timed out"; exit 1

(* ocamlfind opt -package lwt.unix -linkpkg example.ml && ./a.out *)

In the program, functions such as Lwt_io.write create promises. The let* ... in construct is used to wait for a promise to become determined; the code after in is scheduled to run in a "callback." Lwt.pick races promises against each other, and behaves as the first one to complete. Lwt_main.run forces the whole promise-computation network to be executed. All the visible OCaml code is run in a single thread, but Lwt internally uses a combination of worker threads and non-blocking file descriptors to resolve in parallel the promises that do I/O.


Overview

Lwt compiles to native code on Linux, macOS, Windows, and other systems. It's also routinely compiled to JavaScript for the front end and Node by js_of_ocaml.

In Lwt,

  • The core library Lwt provides promises...
  • ...and a few pure-OCaml helpers, such as promise-friendly mutexes, condition variables, and mvars.
  • There is a big Unix binding, Lwt_unix that binds almost every Unix system call. A higher-level module Lwt_io provides nice I/O channels.
  • Lwt_process is for subprocess handling.
  • Lwt_preemptive spawns system threads.
  • The PPX syntax allows using all of the above without going crazy!
  • There are also some other helpers, such as Lwt_react for reactive programming. See the table of contents on the linked manual pages!

Installing

  1. Use your system package manager to install a development libev package. It is often called libev-dev or libev-devel.
  2. opam install conf-libev lwt

Documentation

We are currently working on improving the Lwt documentation (drastically; we are rewriting the manual). In the meantime:

  • The current manual can be found here.
  • Mirage has a nicely-written Lwt tutorial.
  • An example of a simple server written in Lwt.
  • Concurrent Programming with Lwt is a nice source of Lwt examples. They are translations of code from the excellent Real World OCaml, but are just as useful if you are not reading the book.

Note: much of the current manual refers to 'a Lwt.t as "lightweight threads" or just "threads." This will be fixed in the new manual. 'a Lwt.t is a promise, and has nothing to do with system or preemptive threads.


Contact

Open an issue, visit Discord chat, ask on discuss.ocaml.org, or on Stack Overflow.

Release announcements are made in /r/ocaml, and on discuss.ocaml.org. Watching the repo for "Releases only" is also an option.


Contributing

  • CONTRIBUTING.md contains tips for working on the code, such as how to check the code out, how review works, etc. There is also a high-level outline of the code base.
  • Ask us anything, whether it's about working on Lwt, or any question at all about it :)
  • The documentation always needs proofreading and fixes.
  • You are welcome to pick up any other issue, review a PR, add your opinion, etc.
  • Any feedback is welcome, including how to make contributing easier!

Libraries to use with Lwt

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].