Synchronous vs. Asynchronous Programming Jan Pascal Maas Institute - - PowerPoint PPT Presentation

synchronous vs asynchronous programming
SMART_READER_LITE
LIVE PREVIEW

Synchronous vs. Asynchronous Programming Jan Pascal Maas Institute - - PowerPoint PPT Presentation

Synchronous vs. Asynchronous Programming Jan Pascal Maas Institute for Software Engineering and Programming Languages University of Luebeck 25. January 2016 Seminar Concepts of Programming Languages Introduction 3 Synchronous vs.


slide-1
SLIDE 1

Synchronous vs. Asynchronous Programming

Jan Pascal Maas

Institute for Software Engineering and Programming Languages University of Luebeck

  • 25. January 2016

Seminar Concepts of Programming Languages

slide-2
SLIDE 2

Introduction Classification Approaches Conclusion

Agenda

1

Introduction

2

Classification Synchronous Programming Asynchronous Programming

3

Approaches Synchronous Asynchronous

4

Conclusion

  • J. P. Maas (University of Luebeck)

Synchronous vs. Asynchronous Programming 2/ 30

slide-3
SLIDE 3

Introduction Classification Approaches Conclusion

Introduction

programs rely on multiple constraints

especially concurrency and communication increase complexity

concurrency is largely explored

general programming paradigms can be used multiple approaches exist

  • J. P. Maas (University of Luebeck)

Synchronous vs. Asynchronous Programming 3/ 30

slide-4
SLIDE 4

Introduction Classification Approaches Conclusion

Introduction—Communication

increases complexity largely

Example (Air Traffic Control System)

information is known beforehand

all tasks can run independently low complexity

information and communication during execution

system needs to be capable of accepting and processing information requires high amount of synchronized communication

  • J. P. Maas (University of Luebeck)

Synchronous vs. Asynchronous Programming 4/ 30

slide-5
SLIDE 5

Introduction Classification Approaches Conclusion

Introduction—Communication

increases complexity largely

Example (Air Traffic Control System)

information is known beforehand

all tasks can run independently low complexity

information and communication during execution

system needs to be capable of accepting and processing information requires high amount of synchronized communication

  • J. P. Maas (University of Luebeck)

Synchronous vs. Asynchronous Programming 4/ 30

slide-6
SLIDE 6

Introduction Classification Approaches Conclusion

Introduction—Paradigms to Synchronize

blocking (scheduler-based): block task to use resources differently

blocked task ensures resuming of computation specific synchronization condition required

busy-waiting: use of an evaluation loop

reevaluated specific condition till it becomes true

  • J. P. Maas (University of Luebeck)

Synchronous vs. Asynchronous Programming 5/ 30

slide-7
SLIDE 7

Introduction Classification Approaches Conclusion

Synchronous Programming

applies scheduler-based synchronization

blocks a task if it is necessary resources can be used for a different task if needed resources are available, computation continues

ensures correctness of the system

  • J. P. Maas (University of Luebeck)

Synchronous vs. Asynchronous Programming 6/ 30

slide-8
SLIDE 8

Introduction Classification Approaches Conclusion

Figure: Synchronous blocking model [1].

  • J. P. Maas (University of Luebeck)

Synchronous vs. Asynchronous Programming 7/ 30

slide-9
SLIDE 9

Introduction Classification Approaches Conclusion

Figure: Asynchronous model [1].

  • J. P. Maas (University of Luebeck)

Synchronous vs. Asynchronous Programming 8/ 30

slide-10
SLIDE 10

Introduction Classification Approaches Conclusion

Asynchronous Programming

uses busy-waiting

re-evaluation of a specific condition if true, condition depends on an external system actions to compute are specified before execution

main thread continues running actions that depend on external system(s) are executed on different thread

  • J. P. Maas (University of Luebeck)

Synchronous vs. Asynchronous Programming 9/ 30

slide-11
SLIDE 11

Introduction Classification Approaches Conclusion

Synchronous Approaches—LUSTRE

data-flow oriented language focussed on temporal correctness variables are treated as infinite sequences (x0 = e0, x1 = e1, ..., xn = en,...)

  • J. P. Maas (University of Luebeck)

Synchronous vs. Asynchronous Programming 10/ 30

slide-12
SLIDE 12

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

slide-13
SLIDE 13

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

slide-14
SLIDE 14

Introduction Classification Approaches Conclusion

LUSTRE—Synchronization

when is used to create streams streams allow synchronization of the program to 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

slide-15
SLIDE 15

Introduction Classification Approaches Conclusion

LUSTRE—Synchronization

when is used to create streams streams allow synchronization of the program to 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

slide-16
SLIDE 16

Introduction Classification Approaches Conclusion

SIGNAL

concept similar to LUSTRE allows explicit synchronization using synchro merging of two signals with default

  • J. P. Maas (University of Luebeck)

Synchronous vs. Asynchronous Programming 13/ 30

slide-17
SLIDE 17

Introduction Classification Approaches Conclusion

ESTEREL

imperative language variables are called signals reaction: process of computing output based on input fixed status and current value (initially ) in the same reaction

  • J. P. Maas (University of Luebeck)

Synchronous vs. Asynchronous Programming 14/ 30

slide-18
SLIDE 18

Introduction Classification Approaches Conclusion

ESTEREL—Synchronization

emit: sending output signals watching: await specific signal present: detects for presence of a signal do I1 O1 watching I2 ; emit O2

  • J. P. Maas (University of Luebeck)

Synchronous vs. Asynchronous Programming 15/ 30

slide-19
SLIDE 19

Introduction Classification Approaches Conclusion

ESTEREL—Synchronization

emit: sending output signals watching: await specific signal present: detects for presence of a signal do I1 −> O1 watching I2 ; emit O2

  • J. P. Maas (University of Luebeck)

Synchronous vs. Asynchronous Programming 15/ 30

slide-20
SLIDE 20

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

slide-21
SLIDE 21

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

slide-22
SLIDE 22

Introduction Classification Approaches Conclusion

Asynchronous Approaches—Event Loop

inversion of control efficiency and scalability control over switching between application activities relies on notification facilities application handles occurrence of events commonly used: Node.js

  • J. P. Maas (University of Luebeck)

Synchronous vs. Asynchronous Programming 17/ 30

slide-23
SLIDE 23

Introduction Classification Approaches Conclusion Client 1 Client 2 ⋮ Client n Request 1 Request 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 is processed 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 one Thread

Figure: Node.js processing model [2].

  • J. P. Maas (University of Luebeck)

Synchronous vs. Asynchronous Programming 18/ 30

slide-24
SLIDE 24

Introduction Classification Approaches Conclusion Client 1 Client 2 ⋮ Client n Request 1 Request 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 is processed 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 one Thread

Figure: Node.js processing model [2].

  • J. P. Maas (University of Luebeck)

Synchronous vs. Asynchronous Programming 18/ 30

slide-25
SLIDE 25

Introduction Classification Approaches Conclusion Client 1 Client 2 ⋮ Client n Request 1 Request 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 is processed 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 one Thread

Figure: Node.js processing model [2].

  • J. P. Maas (University of Luebeck)

Synchronous vs. Asynchronous Programming 18/ 30

slide-26
SLIDE 26

Introduction Classification Approaches Conclusion Client 1 Client 2 ⋮ Client n Request 1 Request 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 is processed 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 one Thread

Figure: Node.js processing model [2].

  • J. P. Maas (University of Luebeck)

Synchronous vs. Asynchronous Programming 18/ 30

slide-27
SLIDE 27

Introduction Classification Approaches Conclusion Client 1 Client 2 ⋮ Client n Request 1 Request 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 is processed 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 one Thread

Figure: Node.js processing model [2].

  • J. P. Maas (University of Luebeck)

Synchronous vs. Asynchronous Programming 18/ 30

slide-28
SLIDE 28

Introduction Classification Approaches Conclusion Client 1 Client 2 ⋮ Client n Request 1 Request 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 is processed 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 one Thread

Figure: Node.js processing model [2].

  • J. P. Maas (University of Luebeck)

Synchronous vs. Asynchronous Programming 18/ 30

slide-29
SLIDE 29

Introduction Classification Approaches Conclusion Client 1 Client 2 ⋮ Client n Request 1 Request 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 is processed 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 one Thread

Figure: Node.js processing model [2].

  • J. P. Maas (University of Luebeck)

Synchronous vs. Asynchronous Programming 18/ 30

slide-30
SLIDE 30

Introduction Classification Approaches Conclusion

Continuations

Example

public int Divide ( int top , int bottom) { i f (bottom==0) { throw new InvalidOperationException (” div ␣by␣0” ) ; } else { return top/bottom ; } } public bool IsEven ( int aNumber) { var isEven = (aNumber % 2 == 0); return isEven ; }

  • J. P. Maas (University of Luebeck)

Synchronous vs. Asynchronous Programming 19/ 30

slide-31
SLIDE 31

Introduction Classification Approaches Conclusion

Continuations

Example

public T Divide< T >(int top , int bottom , Func < T > ifZero , Func <int ,T > ifSuccess ) { i f (bottom==0) { return ifZero ( ) ; } else { return ifSuccess ( top/bottom ) ; } } public T IsEven< T >(int aNumber , Func <int ,T > ifOdd , Func <int ,T > ifEven ) { i f (aNumber % 2 == 0) { return ifEven (aNumber ) ; } else { return ifOdd (aNumber ) ; } }

  • J. P. Maas (University of Luebeck)

Synchronous vs. Asynchronous Programming 20/ 30

slide-32
SLIDE 32

Introduction Classification Approaches Conclusion

Asynchrony in F#

also implementing event-based paradigm library with new syntactic category aexpr use capability of language to handle different context asynchronous operations are capable of binding core language results

  • J. P. Maas (University of Luebeck)

Synchronous vs. Asynchronous Programming 21/ 30

slide-33
SLIDE 33

Introduction Classification Approaches Conclusion

Asynchrony in F#—Task Generators

sometimes functions need task generators can run asynchronous computations synchronously can 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

slide-34
SLIDE 34

Introduction Classification Approaches Conclusion

Example (Task Generators)

let sleepThenReturnResult = async { printfn "before␣sleep" do! Async.Sleep 5000 return 1000 } let res = Async.RunSynchronously sleepThenReturnResult printfn "result␣=␣%d" res

  • J. P. Maas (University of Luebeck)

Synchronous vs. Asynchronous Programming 23/ 30

slide-35
SLIDE 35

Introduction Classification Approaches Conclusion

Where is the callback run?

in .NET any computation has access to synchronization context any 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

slide-36
SLIDE 36

Introduction Classification Approaches Conclusion

Where is the callback run?

in .NET any computation has access to synchronization context any 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

slide-37
SLIDE 37

Introduction Classification Approaches Conclusion

Asynchronous Resource Clean-Up

language feature: use! allows to directly dispose resources cancellation of operations: implicit propagation of a token

  • J. P. Maas (University of Luebeck)

Synchronous vs. Asynchronous Programming 25/ 30

slide-38
SLIDE 38

Introduction Classification Approaches Conclusion

Synchronous Programming—Pro and Contra

ensures temporal and logical correctness blocking a thread might block complete system requires manual use of synchronization mechanisms may lie outside the control of the programmer

  • J. P. Maas (University of Luebeck)

Synchronous vs. Asynchronous Programming 26/ 30

slide-39
SLIDE 39

Introduction Classification Approaches Conclusion

Synchronous Programming—Pro and Contra

ensures temporal and logical correctness blocking a thread might block complete system requires manual use of synchronization mechanisms may lie outside the control of the programmer

  • J. P. Maas (University of Luebeck)

Synchronous vs. Asynchronous Programming 26/ 30

slide-40
SLIDE 40

Introduction Classification Approaches Conclusion

Asynchronous Programming—Pro and Contra

system stays responsive

  • utperforms synchronous

systems main process is always single-threaded programmer is in control of task suspension multi-threading is not forbidden allows to reduce the syntax to “computation expressions” no need to explicitly ensure temporal correctness program needs to be

  • rganized in smaller steps

no explicit use of multi-threading not all interprocess communication can be reduced to events high complexity without callbacks

  • J. P. Maas (University of Luebeck)

Synchronous vs. Asynchronous Programming 27/ 30

slide-41
SLIDE 41

Introduction Classification Approaches Conclusion

Asynchronous Programming—Pro and Contra

system stays responsive

  • utperforms synchronous

systems main process is always single-threaded programmer is in control of task suspension multi-threading is not forbidden allows to reduce the syntax to “computation expressions” no need to explicitly ensure temporal correctness program needs to be

  • rganized in smaller steps

no explicit use of multi-threading not all interprocess communication can be reduced to events high complexity without callbacks

  • J. P. Maas (University of Luebeck)

Synchronous vs. Asynchronous Programming 27/ 30

slide-42
SLIDE 42

Introduction Classification Approaches Conclusion

Conclusion

Asynchronous programming eliminates some issues from synchronous programming Asynchrony allows the programmer to take control Asynchrony takes care of temporal correctness Synchronous programming is required in some areas Asynchronous programming can not be used at any operation Complexity of Asynchrony can ensure unmaintainable code

  • J. P. Maas (University of Luebeck)

Synchronous vs. Asynchronous Programming 28/ 30

slide-43
SLIDE 43

Introduction Classification Approaches Conclusion

Conclusion

Asynchronous programming eliminates some issues from synchronous programming Asynchrony allows the programmer to take control Asynchrony takes care of temporal correctness Synchronous programming is required in some areas Asynchronous programming can not be used at any operation Complexity of Asynchrony can ensure unmaintainable code

  • J. P. Maas (University of Luebeck)

Synchronous vs. Asynchronous Programming 28/ 30

slide-44
SLIDE 44

Introduction Classification Approaches Conclusion

Thank You for Your attention.

  • J. P. Maas (University of Luebeck)

Synchronous vs. Asynchronous Programming 29/ 30

slide-45
SLIDE 45

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 loop architecture. https://tinyurl.com/jjc7btk, 2015. Accessed: 2015-12-17.

  • J. P. Maas (University of Luebeck)

Synchronous vs. Asynchronous Programming 30/ 30