Skip to content

Await

Name - Description Default Type
<input> Must match the input types of the first shard in the sequence. Any
<output> Will match the output types of the first shard of the sequence. Any
Shards The shards to activate. None Shard[Shard]

Executes a shard or a sequence of shards asynchronously and awaits their completion.

Details

(Await) runs its shards (or sequence of shards) as a separate task that is sent directly to the thread pool, while the rest of the program continues executing (via other scheduled threads). Once this (Await) task thread completes its execution the result of the execution of these inner shards is made available to the program.

This is called asynchronous computation and is used to prevent resource intensive processing (like downloading a large file data from an http server) from holding up the execution of the rest of the program.

Note

(Await) has an alias (||) which is more convenient to use. || also removes the need to use (->) as, unlike (Await), it doesn't require the parameter shards to be grouped together.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
;; Asynchronous execution of shards (using keyword `Await`)
;; Printing order of messages not consistent across program runs --
;; -- as asynchronous shards might complete at different times
(defmesh mesh)

(defloop await-wire
(Await
  ;:Shards
  (->
    (Msg "Message 1")
    ;; messages might print in any order (1/2/3, 1/3/2, 3/2/1, etc.)
    (Msg "Message 2")
    ;; messages might print in any order (1/2/3, 1/3/2, 3/2/1, etc.)
  )))

(defloop my-wire
    (Msg "Message 3")
    ;; messages might print in any order (1/2/3, 1/3/2, 3/2/1, etc.)
    )

(schedule mesh await-wire)
(schedule mesh my-wire)
(run mesh 1 1)
[info] [my-wire] Message 3
[info] [await-wire] Message 1

 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
;; Asynchronous execution of shards (using alias `||`)
;; Printing order of messages not consistent across program runs --
;; -- as asynchronous shards might complete at different times
(defmesh mesh)

(defloop await-wire
(||
  ;:Shards
    (Msg "Message 1")
    ;; messages might print in any order (1/2/3, 1/3/2, 3/2/1, etc.)
    (Msg "Message 2")
    ;; messages might print in any order (1/2/3, 1/3/2, 3/2/1, etc.)
  ))

(defloop my-wire
    (Msg "Message 3")
    ;; messages might print in any order (1/2/3, 1/3/2, 3/2/1, etc.)
    )

(schedule mesh await-wire)
(schedule mesh my-wire)
(run mesh 1 1)
[info] [my-wire] Message 3
[info] [await-wire] Message 1