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

Post on 02-Jul-2015

258 views 3 download

description

Речь пойдет о двух языках: Python и Javascript. Максим расскажет об асинхронной работе Python и Javascript, что даст возможность слушателям приобщиться к особой касте разработчиков, работающих с Twisted.

transcript

Asynchronous programmingJS and python inside

Maxim Schepelin

Contents

● Python.● What is call stack?● Where callbacks come from?● How asynchronous code execution works?● Summary

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)

Code: Call stack:

main

print_result

square

multiply

def foo(): foo()

if __name__ == '__main__': foo()

Code: Call stack:

main

foo

foo

foo

foo

foo

foo

foo

RuntimeError: maximum recursion depth exceeded

Contents

● Python.● What is call stack?● Where callbacks come from?● How asynchronous code execution works?● Summary

console.log(1);console.log(2);setTimeout(function ( ) { console.log(4);}, 500);console.log(3);

Code: Call stack:

main

console.logsetTimeout

anonymous func

console.log(1);console.log(2);setTimeout(function ( ) { console.log(4);}, 500);console.log(3);

Code: Call stack:

main

console.logsetTimeout

anonymous 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

External APICallback

queue

Async callEvent-loop

console.log(1);console.log(2);setTimeout(function ( ) { console.log(4);}, 0);console.log(3);

Code: Call stack:

main

console.logsetTimeout

anonymous 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&Aschepelin

Maxim Schepelin