Created
October 31, 2017 03:54
-
-
Save littlecodersh/6803d2c3382de9a7793a0189db72f538 to your computer and use it in GitHub Desktop.
Trip coroutine demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time, functools | |
import requests, trip | |
def timeit(fn): | |
start_time = time.time() | |
fn() | |
return time.time() - start_time | |
url = 'http://httpbin.org/get' | |
times = 10 # 100 changed for inland network delay | |
def fetch(): | |
r = [requests.get(url) for i in range(times)] | |
return r | |
@trip.coroutine | |
def async_fetch(): | |
r = yield [trip.get(url) for i in range(times)] | |
raise trip.Return(r) | |
print('Non-trip cost: %ss' % timeit(fetch)) | |
print('Trip cost: %ss' % timeit(functools.partial(trip.run, async_fetch))) | |
# Non-trip cost: 17.90799999237s | |
# Trip cost: 0.172300004959s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment