Module Async_kernel.Throttled

An applicative type for concurrent computations with limited concurrency.

The computation takes place lazily and only when the run function is called.

Compared to how Throttle is typically used, this type lets you avoid an up-front time&memory cost of adding all concurrent jobs to the throttle. In particular you can implement a throttled traversal of a very large data structure without doing a synchronous walk over the entire structure at any given time.

module Deferred := Async_kernel__.Deferred1
type 'a t
include Core.Applicative with type 'a t := 'a t
val return : 'a 'p 'q. 'a -> 'a t

Convert a value to a t.

val apply : 'a 'b 'p 'q. ('a -> 'b) t -> 'a t -> 'b t

Applies the functions in one t to the values in another. Well-behaved applicatives satisfy these "laws", using <*> as infix apply:

  • return Fn.id <*> t is equivalent to t
  • return Fn.compose <*> tf <*> tg <*> tx is equivalent to tf <*> (tg <*> tx)
  • return f <*> return x is equivalent to return (f x)
  • tf <*> return x is equivalent to return (fun f -> f x) <*> tf
val both : 'a 'b 'p 'q. 'a t -> 'b t -> ('a * 'b) t

Combines values in two ts as tuples. Using <*> as infix apply, equivalent to return (fun a b -> a, b) <*> ta <*> tb.

val map : 'a 'b 'p 'q. 'a t -> f:('a -> 'b) -> 'b t

Transforms the contents of a t. Using <*> as infix apply, equivalent to return f <*> t.

val map2 : 'a 'b 'c 'p 'q. 'a t -> 'b t -> f:('a -> 'b -> 'c) -> 'c t

Combines the contents of two ts. Using <*> as infix apply, equivalent to return f <*> ta <*> tb.

val map3 : 'a 'b 'c 'd 'p 'q. 'a t -> 'b t -> 'c t -> f:('a -> 'b -> 'c -> 'd) -> 'd t

Combines the contents of three ts. Using <*> as infix apply, equivalent to return f <*> ta <*> tb <*> tc.

val all : 'a 'p 'q. 'a t list -> 'a list t

Combines a list of t.

val all_unit : 'p 'q. unit t list -> unit t

Combines a list of t whose contents are unimportant.

val (<*>) : 'a 'b 'p 'q. ('a -> 'b) t -> 'a t -> 'b t
val (<*) : 'a 'p 'q. 'a t -> unit t -> 'a t
val (*>) : 'a 'p 'q. unit t -> 'a t -> 'a t
val (>>|) : 'a 'b 'p 'q. 'a t -> ('a -> 'b) -> 'b t
module Applicative_infix : sig ... end
val job : (unit -> 'a Deferred.t) -> 'a t

job f takes a function of type unit -> 'a Defferred.t and converts it to a Throttled type than can be executed with throttling using val run.

The function will be holding one throttle token while running.

val run : 'a t -> max_concurrent_jobs:int -> 'a Deferred.t

run t takes a Throttled type and runs it.

val of_thunk : (unit -> 'a t) -> 'a t

of_thunk thunk converts a function of type unit -> 'a t to type 'a t. Useful for delaying computation.

val both_unit : unit t -> unit t -> unit t

both_unit t1 t2 combines two unit t into one in a way that's more efficent by saving the mapping over the final deferred.

This optimization is important if t1 finishes early but t2 finishes late, since the memory usage between the two events is reduced to 0.