Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Asynchronous Python

Asynchronous Python

A short introduction to asynchronous programming and asyncio, which was introduced in Python 3.4.

Chang-Hung Liang

July 08, 2015
Tweet

More Decks by Chang-Hung Liang

Other Decks in Programming

Transcript

  1. WHY ASYNC? • Synchronous model ◦ CPU idles too much

    • Threaded model ◦ Difficult to code (locks, thread coordination) ◦ Context switching ◦ Idling problem is still there
  2. “ 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
  3. COROUTINES • Write async code like it was sync •

    Make use of yield • Before Python 3.4 we have gevent
  4. 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()
  5. 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()
  6. P/E ratio = Price / Earnings per share Low P/E

    ➭ the stock is cheap PEG = PE ratio / Glassdoor rating
  7. 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
  8. 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
  9. @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
  10. @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