KEMBAR78
Asynchronous programming intro | PDF
Asynchronous programming intro
kakashi@umbocv
Asynchronous Flow
• Callbacks
• Task (threading)
• Future/Promise
• AsyncIO
• Coroutines
• async/await
• ...
Background
Why we need to run program asynchronously
• Some piece of program are slow
• Disk IO and Network IO
• CPU-intensive jobs
Synchronous function
• Caller needs to wait for return
• Can’t do anything else in-between
• Idle time
How network I/O works in Linux system
Terminology
• Blocking I/O
• Non-Blocking I/O
• Synchronous
• Asynchronous
Can you tell me what’s the difference?
8
Before the explanation
In unix world, we need to consider about
User space(thread or process) & Kernel space
e.g when we invoke a read operator
1. Waiting for data to be ready
2. Copying the data from kernel to the process
9
1
0
1
1
1
2
1
3
Answer
Blocking IO & Non-Blocking IO are determined by function call
1. A synchronous I/O operation causes the requesting process to
be blocked until that I/O operation completes;
2. An asynchronous I/O operation does not cause the requesting
process to be blocked;
14
Question
Which is synchronous IO?
15
Concurrency vs Parallelism ?
Asynchronous vs Synchronous ?
17
Synchronous Model
Single Thread
Multi Thread
18
A B C
Thread
A
B
C
Thread 1
Thread 2
Thread 3
Asynchronous Model
Single Thread
Multi Thread
19
T1 T2 T1
Thread
T3 T2 T3
T2 T1
Thread1
T1 T3T1 T3
T6 T5
Thread2
T4 T2T4 T5
T10 T11
Thread3
T11 T9T9 T10
4 Scenarios
1. Synchronous Single Thread
2. Synchronous Multi Thread
3. Asynchronous Single Thread
4. Asynchronous Multi Thread
20
Concurrency
In simple terms, concurrency means processing multiple requests
at a time.
e.g chef can finish multiple tasks at a time.
- chopping
- prepare sauces
- cooking main dishes
...
21
Concurrency
1. Synchronous Single Thread
2. Synchronous Multi Thread
3. Asynchronous Single Thread
4. Asynchronous Multi Thread
22
Concurrency
1. Synchronous Single Thread
2. Synchronous Multi Thread
3. Asynchronous Single Thread
4. Asynchronous Multi Thread
23
Event Loop
Event loop
25
python-cookbook-3rd
Event loop
Port binding, add
FD to watch list
FD0 FD1 FD2 FD3
Select watch list
Wait for activity
in the watch list
by using Select
Client connects,
add to watch list
Check all FD for
available data
Client disconnects,
remove from watch list
Execute handler to
deal with fd
https://blog.logentries.com/2016/07/what-exactly-is-an-event-loop/
Event loop
1. Take advantage of IO multiplexing
a. select O(n)
b. epoll O(1)
2. Eliminate idle time
27
Event Loop in javascript
Client
Client
Client
Client
Event loop
(Single Thread,
non-blocking)
Thread
Thread
Thread
Thread
Long running task
Blocking IO task
Callback
Event loop
console.log(‘hi’)
setTimeout(function() {
console.log(‘there’)
}, 5000)
console.log(‘umbocv’)
Stack
Main
Task queue
console
webapi
Event loop
console.log(‘hi’)
setTimeout(function() {
console.log(‘there’)
}, 5000)
console.log(‘umbocv’)
Stack
Main
Task queue
Console
hi
webapi
setTimeout cbAsync func
With callback
Event loop
console.log(‘hi’)
setTimeout(function() {
console.log(‘there’)
}, 5000)
console.log(‘umbocv’)
Stack
Main
Task queue
Console
hi
webapi
Timer(5000) cb
Event loop
console.log(‘hi’)
setTimeout(function() {
console.log(‘there’)
}, 5000)
console.log(‘umbocv’)
Stack
Main
Task queue
Console
hi
umbocv
webapi
Timer(5000) cb
Event loop
console.log(‘hi’)
setTimeout(function() {
console.log(‘there’)
}, 5000)
console.log(‘umbocv’)
Stack
Main
Task queue
Console
hi
umbocv
webapi
cb
console.log(‘hi’)
setTimeout(function() {
console.log(‘there’)
}, 5000)
console.log(‘umbocv’)
Stack
Task queue
Console
hi
umbocv
webapi
cb
Event loop
Event loop
console.log(‘hi’)
setTimeout(function() {
console.log(‘there’)
}, 5000)
console.log(‘umbocv’)
Stack
Task queue
Console
hi
umbocv
there
webapi
Coroutine
Coroutine
Couroutine are computer program components
that generalize subroutines to allow multiple entry points
for suspending and resuming execution at
certain locations
https://en.wikipedia.org/wiki/Coroutine
Coroutine
function *test() {
console.log(‘there’);
var x = yield;
console.log(‘hi’ + x);
var y = yield;
console.log(‘hi + y);
}
var tester = test()
tester.next() // there
tester.next(‘joe’) // hi joe
tester.next(‘daniel’) // hi daniel
Main Coroutine1
yield
yield
spawn
switch
Coroutine
function *request() {
var resp = yield* getUrlAsync(‘www.umbocv.com’)
return resp
}
var resp_json = request().next().value
When you run asynchronous function,
Use yield pass control to other coroutine
Async/Await
async/await
async function getUrlAsync() {
....
}
async function request() {
const resp = await getUrlAsync(‘www.umbocv.com’)
return resp
}
var resp_json = request()
Use await to invoke asynchronous function and pass control to
another function.
Reference
• https://codewala.net/2015/07/29/concurrency-vs-multi-threading-vs-asynchronou
s-programming-explained/
• https://x.st/javascript-coroutines/
• Javascript eventloop detailed explanation
• https://docs.microsoft.com/en-us/dotnet/csharp/async
Homework
1. Figure out what’s the benefit of using select & epoll (e.g c10 problem)
2. Libuv (nodejs, gevent)
3. What is green thread and what’s the relationship with coroutine

Asynchronous programming intro