Asynchronous 
programming 
JS and python inside 
Maxim Schepelin
Contents 
● Python. 
● What is call stack? 
● Where callbacks come from? 
● How asynchronous code execution works? 
● Summary
Code: Call stack: 
def multiply(x, y): 
return x * y 
def square(x): 
return multiply(x, x) 
def print_result(n): 
result = square(n) 
sys.stdout.write(result) 
if __name__ == '__main__': 
print_result(4) 
multiply 
square 
print_result 
main
Code: Call stack: 
def foo(): 
foo() 
if __name__ == '__main__': 
foo() 
foo 
foo 
foo 
foo 
foo 
foo 
foo 
main 
RuntimeError: maximum recursion depth exceeded
Contents 
● Python. 
● What is call stack? 
● Where callbacks come from? 
● How asynchronous code execution works? 
● Summary
Code: Call stack: 
console.log(1); 
console.log(2); 
setTimeout(function ( ) { 
console.log(4); 
}, 500); 
console.log(3); 
csoentTsiomlee.olougt 
anonymous main 
func
Code: Call stack: 
console.log(1); 
console.log(2); 
setTimeout(function ( ) { 
console.log(4); 
}, 500); 
console.log(3); 
csoentTsiomlee.olougt 
anonymous main 
func 
Browser engine: 
Timer(cb) 
Callback queue: callback 
1 
2 
3
Contents 
● Python. 
● What is call stack? 
● Where callbacks come from? 
● How asynchronous code execution works? 
● Summary
Asynchronous workflow 
Call stack 
Event-loop Async call 
External API 
Callback 
queue
Code: Call stack: 
console.log(1); 
console.log(2); 
setTimeout(function ( ) { 
console.log(4); 
}, 0); 
console.log(3); 
csoentTsiomlee.olougt 
anonymous main 
func 
Browser engine: 
Timer(cb) 
Callback queue: callback 
1 
2 
3
Callbacks are invoked 
only when call stack is 
emtpy!
Contents 
● Python. 
● What is call stack? 
● Where callbacks come from? 
● How asynchronous code execution works? 
● Summary
Summary 
● Asynchromous != multithreading 
● Heavy things blocks the event-loop 
● It’s still single thread
Q&A 
schepelin 
Maxim Schepelin

Про асинхронность / Максим Щепелин / Web Developer Wargaming

  • 1.
    Asynchronous programming JSand python inside Maxim Schepelin
  • 3.
    Contents ● Python. ● What is call stack? ● Where callbacks come from? ● How asynchronous code execution works? ● Summary
  • 4.
    Code: Call stack: def multiply(x, y): return x * y def square(x): return multiply(x, x) def print_result(n): result = square(n) sys.stdout.write(result) if __name__ == '__main__': print_result(4) multiply square print_result main
  • 5.
    Code: Call stack: def foo(): foo() if __name__ == '__main__': foo() foo foo foo foo foo foo foo main RuntimeError: maximum recursion depth exceeded
  • 6.
    Contents ● Python. ● What is call stack? ● Where callbacks come from? ● How asynchronous code execution works? ● Summary
  • 7.
    Code: Call stack: console.log(1); console.log(2); setTimeout(function ( ) { console.log(4); }, 500); console.log(3); csoentTsiomlee.olougt anonymous main func
  • 8.
    Code: Call stack: console.log(1); console.log(2); setTimeout(function ( ) { console.log(4); }, 500); console.log(3); csoentTsiomlee.olougt anonymous main func Browser engine: Timer(cb) Callback queue: callback 1 2 3
  • 9.
    Contents ● Python. ● What is call stack? ● Where callbacks come from? ● How asynchronous code execution works? ● Summary
  • 11.
    Asynchronous workflow Callstack Event-loop Async call External API Callback queue
  • 12.
    Code: Call stack: console.log(1); console.log(2); setTimeout(function ( ) { console.log(4); }, 0); console.log(3); csoentTsiomlee.olougt anonymous main func Browser engine: Timer(cb) Callback queue: callback 1 2 3
  • 13.
    Callbacks are invoked only when call stack is emtpy!
  • 14.
    Contents ● Python. ● What is call stack? ● Where callbacks come from? ● How asynchronous code execution works? ● Summary
  • 15.
    Summary ● Asynchromous!= multithreading ● Heavy things blocks the event-loop ● It’s still single thread
  • 16.