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()
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
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
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
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