Slide 1

Slide 1 text

ASYNCHRONOUS PYTHON

Slide 2

Slide 2 text

WHY ASYNC?

Slide 3

Slide 3 text

SYNCHRONOUS MODEL Credit: http://krondo.com/?p=1209

Slide 4

Slide 4 text

SYNCHRONOUS MODEL Credit: http://krondo.com/?p=1209

Slide 5

Slide 5 text

THREADED MODEL Credit: http://krondo.com/?p=1209

Slide 6

Slide 6 text

ASYNCHRONOUS MODEL Credit: http://krondo.com/?p=1209

Slide 7

Slide 7 text

WHY ASYNC? ● Synchronous model ○ CPU idles too much ● Threaded model ○ Difficult to code (locks, thread coordination) ○ Context switching ○ Idling problem is still there

Slide 8

Slide 8 text

UNDER THE SURFACE select, poll etc (Unix) IOCP (Windows, Solaris)

Slide 9

Slide 9 text

CALLBACK vs. COROUTINE

Slide 10

Slide 10 text

CALLBACKS Twisted, Tornado JavaScript

Slide 11

Slide 11 text

CALLBACK HELL

Slide 12

Slide 12 text

“ It requires superhuman discipline to write readable code in callbacks and if you don’t believe me look at any piece of JavaScript code. - Guido van Rossum Source: https://www.youtube.com/watch?v=1coLC-MUCJc

Slide 13

Slide 13 text

COROUTINES ● Write async code like it was sync ● Make use of yield ● Before Python 3.4 we have gevent

Slide 14

Slide 14 text

asyncio codenamed “tulip” But now in Python 3.4 we have

Slide 15

Slide 15 text

HELLO WORLD (Callback Style) import asyncio def hello_world(loop): print('Hello World') loop.stop() loop = asyncio.get_event_loop() # Schedule a call to hello_world() loop.call_soon(hello_world, loop) # Blocking call interrupted by loop.stop() loop.run_forever() loop.close()

Slide 16

Slide 16 text

HELLO WORLD (Coroutine Style) import asyncio @asyncio.coroutine def hello_world(): print("Hello World!") loop = asyncio.get_event_loop() # Blocking call which returns when the hello_world() # coroutine is done loop.run_until_complete(hello_world()) loop.close()

Slide 17

Slide 17 text

EXAMPLE: STOCK VALUATION WITH GLASSDOOR RATING

Slide 18

Slide 18 text

P/E ratio = Price / Earnings per share Low P/E ➭ the stock is cheap

Slide 19

Slide 19 text

P/E ratio = Price / Earnings per share Low P/E ➭ the stock is cheap PEG = PE ratio / Glassdoor rating

Slide 20

Slide 20 text

def get_PE(symbol): # Scrape Google Finance and return the PE ratio def get_glassdoor_rating(symbol): # Scrape Glassdoor and return the rating of the company def compute_PEG(symbol): return get_PE(symbol) / get_glassdoor_rating(symbol) # Main program for symbol in [‘AAPL’, ‘FB’, ‘GOOGL’]: print(compute_PEG(symbol)) SYNC IMPLEMENTATION

Slide 21

Slide 21 text

def compute_PEG(symbol): return get_PE(symbol) / get_glassdoor_rating(symbol) class PegThread(Thread): def __init__(self, symbol): self.symbol = symbol def run(self): print(compute_PEG(self.symbol)) # Main program for symbol in [‘AAPL’, ‘FB’, ‘GOOGL’]: PegThread(symbol).start() THREADED IMPLEMENTATION

Slide 22

Slide 22 text

@asyncio.coroutine def compute_PEG(symbol): pe = yield from get_PE(symbol) g = yield from get_glassdoor_rating(symbol) return pe / g @asyncio.coroutine def print_PEG(symbol): peg = yield from compute_PEG(symbol) print(peg) # Main program loop = asyncio.get_event_loop() tasks = [print_PEG(symbol) for symbol in [‘AAPL’, ‘FB’, ‘GOOGL’]] loop.run_until_complete(asyncio.wait(tasks)) ASYNC IMPLEMENTATION

Slide 23

Slide 23 text

yield from f() is equivalent to for x in f(): yield x

Slide 24

Slide 24 text

@asyncio.coroutine def compute_PEG(symbol): pe, g = yield from asyncio.gather(get_PE(symbol), get_glassdoor_rating(symbol)) return pe / g @asyncio.coroutine def print_PEG(symbol): peg = yield from compute_PEG(symbol) print(peg) # Main program loop = asyncio.get_event_loop() tasks = [print_PEG(symbol) for symbol in [‘AAPL’, ‘FB’, ‘GOOGL’]] loop.run_until_complete(asyncio.wait(tasks)) ASYNC IMPLEMENTATION II

Slide 25

Slide 25 text

DEMO http://github.com/eliangcs/asyncio-demo

Slide 26

Slide 26 text

asyncio IS READY!

Slide 27

Slide 27 text

“aio” FAMILY aiohttp aiohttp.web aiopg aioredis aiomysql aioes ...

Slide 28

Slide 28 text

SUMMARY

Slide 29

Slide 29 text

Synchronous programming sucks

Slide 30

Slide 30 text

Threaded programming sucks

Slide 31

Slide 31 text

Callback-style async programming sucks

Slide 32

Slide 32 text

Coroutine-style async programming ROCKS!

Slide 33

Slide 33 text

asyncio IS THE FUTURE