Skip to content

Instantly share code, notes, and snippets.

@finnigja
Created November 8, 2018 18:19
Show Gist options
  • Save finnigja/77a86ac257c3df420b91c2ac1ead2c0d to your computer and use it in GitHub Desktop.
Save finnigja/77a86ac257c3df420b91c2ac1ead2c0d to your computer and use it in GitHub Desktop.
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