Instantly share code, notes, and snippets.

Embed
What would you like to do?
Playing with gevent
#!/usr/bin/env python
from gevent import monkey, joinall, spawn
monkey.patch_all()
import requests
import sys
import time
REQUEST_COUNT = int(sys.argv[1:2][0]) or 20
REQUEST_URL = sys.argv[2:3][0] or 'https://google.com'
targets = range(0, REQUEST_COUNT)
def get(url):
start_request = time.time()
result = requests.get(url).status_code
return (result, time.time() - start_request)
print('Target: {}\nRequest count: {}'.format(REQUEST_URL, REQUEST_COUNT))
print('Testing synchronous requests...')
start_loop = time.time()
results = [get(REQUEST_URL) for i in targets]
print(' {0:20} {1:.2f} seconds'.format('Total elapsed time', time.time() - start_loop))
times = [request_time for (request_status, request_time) in results]
print(' {0:20} {1:.2f} seconds'.format('Average request time', sum(times)/len(times)))
print('Testing asynchronous requests (gevent)...')
start_loop = time.time()
results = [result.value for result in joinall([spawn(get, REQUEST_URL) for i in targets])]
print(' {0:20} {1:.2f} seconds'.format('Total elapsed time', time.time() - start_loop))
times = [request_time for (request_status, request_time) in results]
print(' {0:20} {1:.2f} seconds'.format('Average request time', sum(times)/len(times)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment