Jan Pascal Maas - uni-luebeck.de · Seminar Concepts of Programming Languages. Introduction...
Transcript of Jan Pascal Maas - uni-luebeck.de · Seminar Concepts of Programming Languages. Introduction...
Synchronous vs. Asynchronous Programming
Jan Pascal Maas
Institute for Software Engineering and Programming LanguagesUniversity of Luebeck
25. January 2016Seminar Concepts of Programming Languages
Introduction Classification Approaches Conclusion
Agenda
1 Introduction
2 ClassificationSynchronous ProgrammingAsynchronous Programming
3 ApproachesSynchronousAsynchronous
4 Conclusion
J. P. Maas (University of Luebeck) Synchronous vs. Asynchronous Programming 2/ 30
Introduction Classification Approaches Conclusion
Introduction
programs rely on multiple constraintsespecially concurrency and communication increase complexity
concurrency is largely exploredgeneral programming paradigms can be usedmultiple approaches exist
J. P. Maas (University of Luebeck) Synchronous vs. Asynchronous Programming 3/ 30
Introduction Classification Approaches Conclusion
Introduction—Communication
increases complexity largely
Example (Air Traffic Control System)information is known beforehand
all tasks can run independentlylow complexity
information and communication during executionsystem needs to be capable of accepting and processing informationrequires high amount of synchronized communication
J. P. Maas (University of Luebeck) Synchronous vs. Asynchronous Programming 4/ 30
Introduction Classification Approaches Conclusion
Introduction—Communication
increases complexity largely
Example (Air Traffic Control System)information is known beforehand
all tasks can run independentlylow complexity
information and communication during executionsystem needs to be capable of accepting and processing informationrequires high amount of synchronized communication
J. P. Maas (University of Luebeck) Synchronous vs. Asynchronous Programming 4/ 30
Introduction Classification Approaches Conclusion
Introduction—Paradigms to Synchronize
blocking (scheduler-based): block task to use resources differentlyblocked task ensures resuming of computationspecific synchronization condition required
busy-waiting: use of an evaluation loopreevaluated specific condition till it becomes true
J. P. Maas (University of Luebeck) Synchronous vs. Asynchronous Programming 5/ 30
Introduction Classification Approaches Conclusion
Synchronous Programming
applies scheduler-based synchronizationblocks a task if it is necessaryresources can be used for a different taskif needed resources are available, computation continues
ensures correctness of the system
J. P. Maas (University of Luebeck) Synchronous vs. Asynchronous Programming 6/ 30
Introduction Classification Approaches Conclusion
Figure: Synchronous blocking model [1].
J. P. Maas (University of Luebeck) Synchronous vs. Asynchronous Programming 7/ 30
Introduction Classification Approaches Conclusion
Figure: Asynchronous model [1].
J. P. Maas (University of Luebeck) Synchronous vs. Asynchronous Programming 8/ 30
Introduction Classification Approaches Conclusion
Asynchronous Programming
uses busy-waitingre-evaluation of a specific conditionif true, condition depends on an external systemactions to compute are specified before execution
main thread continues runningactions that depend on external system(s) are executed on differentthread
J. P. Maas (University of Luebeck) Synchronous vs. Asynchronous Programming 9/ 30
Introduction Classification Approaches Conclusion
Synchronous Approaches—LUSTRE
data-flow oriented languagefocussed on temporal correctnessvariables are treated as infinite sequences
(x0 = e0, x1 = e1, . . . , xn = en, . . . )
J. P. Maas (University of Luebeck) Synchronous vs. Asynchronous Programming 10/ 30
Introduction Classification Approaches Conclusion
LUSTRE—Operators
Let X = (x0, x1, . . . , xn, . . . ) and Y = (y0, y1, . . . , yn, . . . ).pre(X) = (nil, x0, x1, . . . , xn−1, . . . )X −> Y = (x0, y1, . . . , yn, . . . )
E =( e0 e1 e2 e3 e4 e5 . . . )B =( tt ff tt tt ff ff . . . )
X = E when B =( x0 = e0 x1 = e2 x2 = e3 . . . )Y = current(X) =( e0 e0 e2 e3 e3 e3 . . . )
J. P. Maas (University of Luebeck) Synchronous vs. Asynchronous Programming 11/ 30
Introduction Classification Approaches Conclusion
LUSTRE—Operators
Let X = (x0, x1, . . . , xn, . . . ) and Y = (y0, y1, . . . , yn, . . . ).pre(X) = (nil, x0, x1, . . . , xn−1, . . . )X −> Y = (x0, y1, . . . , yn, . . . )
E =( e0 e1 e2 e3 e4 e5 . . . )B =( tt ff tt tt ff ff . . . )
X = E when B =( x0 = e0 x1 = e2 x2 = e3 . . . )Y = current(X) =( e0 e0 e2 e3 e3 e3 . . . )
J. P. Maas (University of Luebeck) Synchronous vs. Asynchronous Programming 11/ 30
Introduction Classification Approaches Conclusion
LUSTRE—Synchronization
when is used to create streamsstreams allow synchronization of the programto synchronize differently clocked streams, current is used
assertions generalize equations → facts to synchronize program
assert not (x and y)
J. P. Maas (University of Luebeck) Synchronous vs. Asynchronous Programming 12/ 30
Introduction Classification Approaches Conclusion
LUSTRE—Synchronization
when is used to create streamsstreams allow synchronization of the programto synchronize differently clocked streams, current is used
assertions generalize equations → facts to synchronize program
assert not (x and y)
J. P. Maas (University of Luebeck) Synchronous vs. Asynchronous Programming 12/ 30
Introduction Classification Approaches Conclusion
SIGNAL
concept similar to LUSTREallows explicit synchronization using synchromerging of two signals with default
J. P. Maas (University of Luebeck) Synchronous vs. Asynchronous Programming 13/ 30
Introduction Classification Approaches Conclusion
ESTEREL
imperative languagevariables are called signalsreaction: process of computing output based on inputfixed status and current value (initially �) in the same reaction
J. P. Maas (University of Luebeck) Synchronous vs. Asynchronous Programming 14/ 30
Introduction Classification Approaches Conclusion
ESTEREL—Synchronization
emit: sending output signalswatching: await specific signalpresent: detects for presence of a signal
doI 1 −> O1
watch ing I 2 ;emit O2
J. P. Maas (University of Luebeck) Synchronous vs. Asynchronous Programming 15/ 30
Introduction Classification Approaches Conclusion
ESTEREL—Synchronization
emit: sending output signalswatching: await specific signalpresent: detects for presence of a signal
doI 1 −> O1
watch ing I 2 ;emit O2
J. P. Maas (University of Luebeck) Synchronous vs. Asynchronous Programming 15/ 30
Introduction Classification Approaches Conclusion
Asynchrony through Concurrency
multiple threads used to perform IO-bound tasks
Problem: Scalability is limited
requirement of different approaches
J. P. Maas (University of Luebeck) Synchronous vs. Asynchronous Programming 16/ 30
Introduction Classification Approaches Conclusion
Asynchrony through Concurrency
multiple threads used to perform IO-bound tasksProblem: Scalability is limited
requirement of different approaches
J. P. Maas (University of Luebeck) Synchronous vs. Asynchronous Programming 16/ 30
Introduction Classification Approaches Conclusion
Asynchronous Approaches—Event Loop
inversion of controlefficiency and scalabilitycontrol over switching between application activitiesrelies on notification facilitiesapplication handles occurrence of eventscommonly used: Node.js
J. P. Maas (University of Luebeck) Synchronous vs. Asynchronous Programming 17/ 30
Introduction Classification Approaches Conclusion
Client 1
Client 2
⋮
Client n
Request 1Request 2. . .Request n
Request 1
Request 2
⋮
Request nRequest 1
Request 2
Request n
T1 T2 . . . Tm
Thread T1
Database
File System
handled by
Blocking IO?
Event Loop
Send Responses
non-blocking isprocessed here
Event Queue Internal Thread Pool
Blocking IO handler
⇐Ô
Request-1
Request-2
Request-n
Response-1
Response-2
Response-n
Pick up Requests from Queue
no
no
yes
non-blocking IO
non-blocking IO
blocking IO
pickup oneThread
Figure: Node.js processing model [2].
J. P. Maas (University of Luebeck) Synchronous vs. Asynchronous Programming 18/ 30
Introduction Classification Approaches Conclusion
Client 1
Client 2
⋮
Client n
Request 1Request 2. . .Request n
Request 1
Request 2
⋮
Request nRequest 1
Request 2
Request n
T1 T2 . . . Tm
Thread T1
Database
File System
handled by
Blocking IO?
Event Loop
Send Responses
non-blocking isprocessed here
Event Queue Internal Thread Pool
Blocking IO handler
⇐Ô
Request-1
Request-2
Request-n
Response-1
Response-2
Response-n
Pick up Requests from Queue
no
no
yes
non-blocking IO
non-blocking IO
blocking IO
pickup oneThread
Figure: Node.js processing model [2].
J. P. Maas (University of Luebeck) Synchronous vs. Asynchronous Programming 18/ 30
Introduction Classification Approaches Conclusion
Client 1
Client 2
⋮
Client n
Request 1Request 2. . .Request n
Request 1
Request 2
⋮
Request n
Request 1
Request 2
Request n
T1 T2 . . . Tm
Thread T1
Database
File System
handled by
Blocking IO?
Event Loop
Send Responses
non-blocking isprocessed here
Event Queue Internal Thread Pool
Blocking IO handler
⇐Ô
Request-1
Request-2
Request-n
Response-1
Response-2
Response-n
Pick up Requests from Queue
no
no
yes
non-blocking IO
non-blocking IO
blocking IO
pickup oneThread
Figure: Node.js processing model [2].
J. P. Maas (University of Luebeck) Synchronous vs. Asynchronous Programming 18/ 30
Introduction Classification Approaches Conclusion
Client 1
Client 2
⋮
Client n
Request 1Request 2. . .Request n
Request 1
Request 2
⋮
Request nRequest 1
Request 2
Request n
T1 T2 . . . Tm
Thread T1
Database
File System
handled by
Blocking IO?
Event Loop
Send Responses
non-blocking isprocessed here
Event Queue Internal Thread Pool
Blocking IO handler
⇐Ô
Request-1
Request-2
Request-n
Response-1
Response-2
Response-n
Pick up Requests from Queue
no
no
yes
non-blocking IO
non-blocking IO
blocking IO
pickup oneThread
Figure: Node.js processing model [2].
J. P. Maas (University of Luebeck) Synchronous vs. Asynchronous Programming 18/ 30
Introduction Classification Approaches Conclusion
Client 1
Client 2
⋮
Client n
Request 1Request 2. . .Request n
Request 1
Request 2
⋮
Request nRequest 1
Request 2
Request n
T1 T2 . . . Tm
Thread T1
Database
File System
handled by
Blocking IO?
Event Loop
Send Responses
non-blocking isprocessed here
Event Queue Internal Thread Pool
Blocking IO handler
⇐Ô
Request-1
Request-2
Request-n
Response-1
Response-2
Response-n
Pick up Requests from Queue
no
no
yes
non-blocking IO
non-blocking IO
blocking IO
pickup oneThread
Figure: Node.js processing model [2].
J. P. Maas (University of Luebeck) Synchronous vs. Asynchronous Programming 18/ 30
Introduction Classification Approaches Conclusion
Client 1
Client 2
⋮
Client n
Request 1Request 2. . .Request n
Request 1
Request 2
⋮
Request nRequest 1
Request 2
Request n
T1 T2 . . . Tm
Thread T1
Database
File System
handled by
Blocking IO?
Event Loop
Send Responses
non-blocking isprocessed here
Event Queue Internal Thread Pool
Blocking IO handler
⇐Ô
Request-1
Request-2
Request-n
Response-1
Response-2
Response-n
Pick up Requests from Queue
no
no
yes
non-blocking IO
non-blocking IO
blocking IO
pickup oneThread
Figure: Node.js processing model [2].
J. P. Maas (University of Luebeck) Synchronous vs. Asynchronous Programming 18/ 30
Introduction Classification Approaches Conclusion
Client 1
Client 2
⋮
Client n
Request 1Request 2. . .Request n
Request 1
Request 2
⋮
Request nRequest 1
Request 2
Request n
T1 T2 . . . Tm
Thread T1
Database
File System
handled by
Blocking IO?
Event Loop
Send Responses
non-blocking isprocessed here
Event Queue Internal Thread Pool
Blocking IO handler
⇐Ô
Request-1
Request-2
Request-n
Response-1
Response-2
Response-n
Pick up Requests from Queue
no
no
yes
non-blocking IO
non-blocking IO
blocking IO
pickup oneThread
Figure: Node.js processing model [2].
J. P. Maas (University of Luebeck) Synchronous vs. Asynchronous Programming 18/ 30
Introduction Classification Approaches Conclusion
Continuations
Examplepub l i c i n t Div ide ( i n t top , i n t bottom ){
i f ( bottom==0){
throw new Inva l i dOperat ionExcept ion ( ” d iv ␣by␣0” ) ;}e l s e{
return top/bottom ;}
}
pub l i c bool IsEven ( i n t aNumber){
var i sEven = (aNumber % 2 == 0 ) ;return i sEven ;
}
J. P. Maas (University of Luebeck) Synchronous vs. Asynchronous Programming 19/ 30
Introduction Classification Approaches Conclusion
Continuations
Examplepub l i c T Divide<T>(i n t top , i n t bottom , Func<T> i fZe ro , Func<int ,T> i f S u c c e s s ){
i f ( bottom==0){
return i f Z e r o ( ) ;}e l s e{
return i f S u c c e s s ( top/bottom ) ;}
}
pub l i c T IsEven<T>(i n t aNumber , Func<int ,T> ifOdd , Func<int ,T> i fEven ){
i f (aNumber % 2 == 0){
return i fEven (aNumber ) ;}e l s e{
return i fOdd (aNumber ) ;}
}
J. P. Maas (University of Luebeck) Synchronous vs. Asynchronous Programming 20/ 30
Introduction Classification Approaches Conclusion
Asynchrony in F#
also implementing event-based paradigmlibrary with new syntactic category aexpruse capability of language to handle different contextasynchronous operations are capable of binding core language results
J. P. Maas (University of Luebeck) Synchronous vs. Asynchronous Programming 21/ 30
Introduction Classification Approaches Conclusion
Asynchrony in F#—Task Generators
sometimes functions need task generatorscan run asynchronous computations synchronouslycan be run as a co-routine if it does not produce a result
J. P. Maas (University of Luebeck) Synchronous vs. Asynchronous Programming 22/ 30
Introduction Classification Approaches Conclusion
Example (Task Generators)
let sleepThenReturnResult =async { printfn "before␣sleep"do! Async.Sleep 5000return 1000
}
let res = Async.RunSynchronously sleepThenReturnResultprintfn "result␣=␣%d" res
J. P. Maas (University of Luebeck) Synchronous vs. Asynchronous Programming 23/ 30
Introduction Classification Approaches Conclusion
Where is the callback run?
in .NET any computation has access to synchronization contextany callback is running “somewhere”
can be abused to run callbacks based on function closures
J. P. Maas (University of Luebeck) Synchronous vs. Asynchronous Programming 24/ 30
Introduction Classification Approaches Conclusion
Where is the callback run?
in .NET any computation has access to synchronization contextany callback is running “somewhere”
can be abused to run callbacks based on function closures
J. P. Maas (University of Luebeck) Synchronous vs. Asynchronous Programming 24/ 30
Introduction Classification Approaches Conclusion
Asynchronous Resource Clean-Up
language feature: use!allows to directly dispose resourcescancellation of operations: implicit propagation of a token
J. P. Maas (University of Luebeck) Synchronous vs. Asynchronous Programming 25/ 30
Introduction Classification Approaches Conclusion
Synchronous Programming—Pro and Contra
ensures temporal and logicalcorrectness
blocking a thread might blockcomplete systemrequires manual use ofsynchronization mechanismsmay lie outside the control ofthe programmer
J. P. Maas (University of Luebeck) Synchronous vs. Asynchronous Programming 26/ 30
Introduction Classification Approaches Conclusion
Synchronous Programming—Pro and Contra
ensures temporal and logicalcorrectness
blocking a thread might blockcomplete systemrequires manual use ofsynchronization mechanismsmay lie outside the control ofthe programmer
J. P. Maas (University of Luebeck) Synchronous vs. Asynchronous Programming 26/ 30
Introduction Classification Approaches Conclusion
Asynchronous Programming—Pro and Contra
system stays responsiveoutperforms synchronoussystemsmain process is alwayssingle-threadedprogrammer is in control oftask suspensionmulti-threading is notforbiddenallows to reduce the syntax to“computation expressions”no need to explicitly ensuretemporal correctness
program needs to beorganized in smaller stepsno explicit use ofmulti-threadingnot all interprocesscommunication can bereduced to eventshigh complexity withoutcallbacks
J. P. Maas (University of Luebeck) Synchronous vs. Asynchronous Programming 27/ 30
Introduction Classification Approaches Conclusion
Asynchronous Programming—Pro and Contra
system stays responsiveoutperforms synchronoussystemsmain process is alwayssingle-threadedprogrammer is in control oftask suspensionmulti-threading is notforbiddenallows to reduce the syntax to“computation expressions”no need to explicitly ensuretemporal correctness
program needs to beorganized in smaller stepsno explicit use ofmulti-threadingnot all interprocesscommunication can bereduced to eventshigh complexity withoutcallbacks
J. P. Maas (University of Luebeck) Synchronous vs. Asynchronous Programming 27/ 30
Introduction Classification Approaches Conclusion
Conclusion
Asynchronous programming eliminates some issues from synchronousprogrammingAsynchrony allows the programmer to take controlAsynchrony takes care of temporal correctness
Synchronous programming is required in some areasAsynchronous programming can not be used at any operationComplexity of Asynchrony can ensure unmaintainable code
J. P. Maas (University of Luebeck) Synchronous vs. Asynchronous Programming 28/ 30
Introduction Classification Approaches Conclusion
Conclusion
Asynchronous programming eliminates some issues from synchronousprogrammingAsynchrony allows the programmer to take controlAsynchrony takes care of temporal correctness
Synchronous programming is required in some areasAsynchronous programming can not be used at any operationComplexity of Asynchrony can ensure unmaintainable code
J. P. Maas (University of Luebeck) Synchronous vs. Asynchronous Programming 28/ 30
Introduction Classification Approaches Conclusion
Thank You for Your attention.
J. P. Maas (University of Luebeck) Synchronous vs. Asynchronous Programming 29/ 30
Introduction Classification Approaches Conclusion
Literature
Dave Peticolas.An introduction to asynchronous programming and twisted.http://krondo.com/?p=1209, 2009.Accessed: 2015-11-07.Rambabu Posa.Node.js processing model – single threaded model with event looparchitecture.https://tinyurl.com/jjc7btk, 2015.Accessed: 2015-12-17.
J. P. Maas (University of Luebeck) Synchronous vs. Asynchronous Programming 30/ 30