Promises - Cornell University•parallel: happening at the same time ... async/await in JavaScript...
Transcript of Promises - Cornell University•parallel: happening at the same time ... async/await in JavaScript...
![Page 1: Promises - Cornell University•parallel: happening at the same time ... async/await in JavaScript and .NET, java.util.concurrent.Future, Clojure, Scala OCamlAsyncand Lwt (and many](https://reader035.fdocuments.us/reader035/viewer/2022070721/5ee15f20ad6a402d666c471f/html5/thumbnails/1.jpg)
Promises
Today’s music: Call Me Maybe by Carly Rae Jepsen
Nate FosterSpring 2019
![Page 2: Promises - Cornell University•parallel: happening at the same time ... async/await in JavaScript and .NET, java.util.concurrent.Future, Clojure, Scala OCamlAsyncand Lwt (and many](https://reader035.fdocuments.us/reader035/viewer/2022070721/5ee15f20ad6a402d666c471f/html5/thumbnails/2.jpg)
ReviewPreviously in 3110: Advanced data structures• Streams• Balanced binary trees• Mutable data structures
Today: • Promises: a data structure and programming
paradigm for concurrency
![Page 3: Promises - Cornell University•parallel: happening at the same time ... async/await in JavaScript and .NET, java.util.concurrent.Future, Clojure, Scala OCamlAsyncand Lwt (and many](https://reader035.fdocuments.us/reader035/viewer/2022070721/5ee15f20ad6a402d666c471f/html5/thumbnails/3.jpg)
Concurrency• Networks have multiple computers• Computers have multiple processors• Processors have multiple cores
...all working semi-independently
...all sharing resources
sequential: non-overlapping in durationconcurrent: overlapping in duration• parallel: happening at the same time• interleaved: rapidly switching between
![Page 4: Promises - Cornell University•parallel: happening at the same time ... async/await in JavaScript and .NET, java.util.concurrent.Future, Clojure, Scala OCamlAsyncand Lwt (and many](https://reader035.fdocuments.us/reader035/viewer/2022070721/5ee15f20ad6a402d666c471f/html5/thumbnails/4.jpg)
ConcurrencyAt any given time, my laptop is...• Streaming music• Running a web server• Syncing with web services• Running OCaml
The OS plays a big role in making it look like those all happen simultaneously
![Page 5: Promises - Cornell University•parallel: happening at the same time ... async/await in JavaScript and .NET, java.util.concurrent.Future, Clojure, Scala OCamlAsyncand Lwt (and many](https://reader035.fdocuments.us/reader035/viewer/2022070721/5ee15f20ad6a402d666c471f/html5/thumbnails/5.jpg)
Concurrency
Applications might also want concurrency:• Web server that handles many clients at once• Scientific calculations that exploit parallel
architecture to get speedup• GUIs that want to respond to users while
doing computation (e.g., rendering) in the background
![Page 6: Promises - Cornell University•parallel: happening at the same time ... async/await in JavaScript and .NET, java.util.concurrent.Future, Clojure, Scala OCamlAsyncand Lwt (and many](https://reader035.fdocuments.us/reader035/viewer/2022070721/5ee15f20ad6a402d666c471f/html5/thumbnails/6.jpg)
Programming models for concurrencyThreads: sequential code for computationPthreads, OpenMP, java.lang.ThreadOCaml Thread
Promises: values that are promised to be computedasync/await in JavaScript and .NET, java.util.concurrent.Future, Clojure, ScalaOCaml Async and Lwt
(and many others)
![Page 7: Promises - Cornell University•parallel: happening at the same time ... async/await in JavaScript and .NET, java.util.concurrent.Future, Clojure, Scala OCamlAsyncand Lwt (and many](https://reader035.fdocuments.us/reader035/viewer/2022070721/5ee15f20ad6a402d666c471f/html5/thumbnails/7.jpg)
PROMISES
![Page 8: Promises - Cornell University•parallel: happening at the same time ... async/await in JavaScript and .NET, java.util.concurrent.Future, Clojure, Scala OCamlAsyncand Lwt (and many](https://reader035.fdocuments.us/reader035/viewer/2022070721/5ee15f20ad6a402d666c471f/html5/thumbnails/8.jpg)
PromisesComputation that promises to produce a value sometime in the future
Aka:• future• delayed• deferred
Lwt: OCaml library for promises
![Page 9: Promises - Cornell University•parallel: happening at the same time ... async/await in JavaScript and .NET, java.util.concurrent.Future, Clojure, Scala OCamlAsyncand Lwt (and many](https://reader035.fdocuments.us/reader035/viewer/2022070721/5ee15f20ad6a402d666c471f/html5/thumbnails/9.jpg)
Promises
A promise – 'a Lwt.t – is like a box:• It starts out empty• At some point in the future, it could be filled
with a value of type 'a• Once it's filled, the box's contents can never
be changed ("write once")
![Page 10: Promises - Cornell University•parallel: happening at the same time ... async/await in JavaScript and .NET, java.util.concurrent.Future, Clojure, Scala OCamlAsyncand Lwt (and many](https://reader035.fdocuments.us/reader035/viewer/2022070721/5ee15f20ad6a402d666c471f/html5/thumbnails/10.jpg)
Resolver
A resolver – 'a Lwt.u – is what fills the box
Terminology:• promise is pending aka sleeping: box is empty• promise is resolved aka returned: box is full• promise is rejected aka failed: box contains exn
Demo
![Page 11: Promises - Cornell University•parallel: happening at the same time ... async/await in JavaScript and .NET, java.util.concurrent.Future, Clojure, Scala OCamlAsyncand Lwt (and many](https://reader035.fdocuments.us/reader035/viewer/2022070721/5ee15f20ad6a402d666c471f/html5/thumbnails/11.jpg)
pending/
sleeping
resolved/
returned
rejected/
failed
Discussion: implement signature for promises
resolve/wakeup
reject/wakeup_exn
![Page 12: Promises - Cornell University•parallel: happening at the same time ... async/await in JavaScript and .NET, java.util.concurrent.Future, Clojure, Scala OCamlAsyncand Lwt (and many](https://reader035.fdocuments.us/reader035/viewer/2022070721/5ee15f20ad6a402d666c471f/html5/thumbnails/12.jpg)
Digression on Cornell history
• ivars = promises+resolvers• Used for parallel computing in language
called Id [Arvind, Nikhil, and Pingali1986]– Keshav Pingali, Cornell CS prof 1986-2006?
• Implemented in Concurrent ML by John Reppy (Cornell PhD 1992)
![Page 13: Promises - Cornell University•parallel: happening at the same time ... async/await in JavaScript and .NET, java.util.concurrent.Future, Clojure, Scala OCamlAsyncand Lwt (and many](https://reader035.fdocuments.us/reader035/viewer/2022070721/5ee15f20ad6a402d666c471f/html5/thumbnails/13.jpg)
Lwt
Typical use of library is to do asynchronous I/O• Launch an I/O operation as a promise• OS helps to resolve promise
Source of parallelism: OS, not OCaml
Demo
![Page 14: Promises - Cornell University•parallel: happening at the same time ... async/await in JavaScript and .NET, java.util.concurrent.Future, Clojure, Scala OCamlAsyncand Lwt (and many](https://reader035.fdocuments.us/reader035/viewer/2022070721/5ee15f20ad6a402d666c471f/html5/thumbnails/14.jpg)
CALLBACKScall me maybe?
![Page 15: Promises - Cornell University•parallel: happening at the same time ... async/await in JavaScript and .NET, java.util.concurrent.Future, Clojure, Scala OCamlAsyncand Lwt (and many](https://reader035.fdocuments.us/reader035/viewer/2022070721/5ee15f20ad6a402d666c471f/html5/thumbnails/15.jpg)
Managing Promises
What if program has many promises "in flight"?• Web server handling many client• Spreadsheet updating many cells• Game updating many enemies
Need a way to manage dependencies of computations upon promises…
Demo
![Page 16: Promises - Cornell University•parallel: happening at the same time ... async/await in JavaScript and .NET, java.util.concurrent.Future, Clojure, Scala OCamlAsyncand Lwt (and many](https://reader035.fdocuments.us/reader035/viewer/2022070721/5ee15f20ad6a402d666c471f/html5/thumbnails/16.jpg)
bind promise callback
bind : 'a Lwt.t-> ('a -> 'b Lwt.t) -> 'b Lwt.t
![Page 17: Promises - Cornell University•parallel: happening at the same time ... async/await in JavaScript and .NET, java.util.concurrent.Future, Clojure, Scala OCamlAsyncand Lwt (and many](https://reader035.fdocuments.us/reader035/viewer/2022070721/5ee15f20ad6a402d666c471f/html5/thumbnails/17.jpg)
promise >>= callback
(>>=) : 'a Lwt.t-> ('a -> 'b Lwt.t) -> 'b Lwt.t
Demo
![Page 18: Promises - Cornell University•parallel: happening at the same time ... async/await in JavaScript and .NET, java.util.concurrent.Future, Clojure, Scala OCamlAsyncand Lwt (and many](https://reader035.fdocuments.us/reader035/viewer/2022070721/5ee15f20ad6a402d666c471f/html5/thumbnails/18.jpg)
Implementing bind
• Store a list of callbacks with each promise• After promise is resolved, Lwt runs callbacks• If promise never resolved (or fails), no callback
![Page 19: Promises - Cornell University•parallel: happening at the same time ... async/await in JavaScript and .NET, java.util.concurrent.Future, Clojure, Scala OCamlAsyncand Lwt (and many](https://reader035.fdocuments.us/reader035/viewer/2022070721/5ee15f20ad6a402d666c471f/html5/thumbnails/19.jpg)
Callback execution
• Single-threaded: one callback runs at a time• Cooperative: callback runs to completion• Nondeterministic: unspecified which runs first
![Page 20: Promises - Cornell University•parallel: happening at the same time ... async/await in JavaScript and .NET, java.util.concurrent.Future, Clojure, Scala OCamlAsyncand Lwt (and many](https://reader035.fdocuments.us/reader035/viewer/2022070721/5ee15f20ad6a402d666c471f/html5/thumbnails/20.jpg)
Upcoming events
• [Tomorrow] A5 released
This is resolved.
THIS IS 3110