Skip to content

Instantly share code, notes, and snippets.

@littlecodersh
Created October 31, 2017 03:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save littlecodersh/6803d2c3382de9a7793a0189db72f538 to your computer and use it in GitHub Desktop.
Save littlecodersh/6803d2c3382de9a7793a0189db72f538 to your computer and use it in GitHub Desktop.
Trip coroutine demo
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