File: executor_pool.mli

package info (click to toggle)
ocaml-eio 1.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,548 kB
  • sloc: ml: 14,608; ansic: 1,237; makefile: 25
file content (59 lines) | stat: -rw-r--r-- 2,403 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
(** An executor pool distributes jobs (functions to execute) among a pool of domain workers (threads).

    Domains are reused and can execute multiple jobs concurrently.
    Jobs are queued up if they cannot be started immediately due to all workers being busy.

    [Eio.Executor_pool] is the recommended way of leveraging OCaml 5's multicore capabilities.
    It is built on top of the low level [Eio.Domain_manager].

    Usually you will only want one pool for an entire application,
    so the pool is typically created when the application starts:

    {[
      let () =
        Eio_main.run @@ fun env ->
        Switch.run @@ fun sw ->
        let pool =
          Eio.Executor_pool.create
            ~sw (Eio.Stdenv.domain_mgr env)
            ~domain_count:4
        in
        main ~pool
    ]}

    The pool starts its domain workers (threads) immediately upon creation.
*)

type t
(** An executor pool. *)

val create :
  sw:Switch.t ->
  domain_count:int ->
  _ Domain_manager.t ->
  t
(** [create ~sw ~domain_count dm] creates a new executor pool.

    The executor pool will not block switch [sw] from completing;
    when the switch finishes, all domain workers and running jobs are cancelled.

    @param domain_count The number of domain workers to create.
                        The total number of domains should not exceed {!Domain.recommended_domain_count} or the number of cores on your system.
                        Additionally, consider reducing this number by 1 if your original domain will be performing CPU intensive work at the same time as the Executor_pool.
*)

val submit : t -> weight:float -> (unit -> 'a) -> ('a, exn) result
(** [submit t ~weight fn] runs [fn ()] using this executor pool.

    The job is added to the back of the queue.

    @param weight This value represents the anticipated proportion of a CPU core used by the job.
                  This value must be >= 0.0 and <= 1.0; Example: given an IO-bound job that averages 2% of a CPU core, pass [~weight:0.02].
                  Each domain worker starts new jobs until the total [~weight] of its running jobs reaches 1.0
 *)

val submit_exn : t -> weight:float -> (unit -> 'a) -> 'a
(** Same as {!submit} but raises if the job fails. *)

val submit_fork : sw:Switch.t -> t -> weight:float -> (unit -> 'a) -> 'a Promise.or_exn
(** Same as {!submit} but returns immediately, without blocking. *)