Created
October 9, 2013 04:30
-
-
Save nkelner/6896241 to your computer and use it in GitHub Desktop.
Watch gevent pwn blocking api calls.
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 gevent.monkey | |
gevent.monkey.patch_socket() | |
import gevent | |
import requests | |
def fetch(pid): | |
response = requests.get('http://timeapi.org/utc/now.json') | |
result = response.json() | |
datetime = result['dateString'] | |
print('Process %s: %s' % (pid, datetime)) | |
return result['dateString'] | |
def synchronous(): | |
for i in range(1,10): | |
fetch(i) | |
def asynchronous(): | |
threads = [] | |
for i in range(1,10): | |
threads.append(gevent.spawn(fetch, i)) | |
gevent.joinall(threads) | |
print('Synchronous:') | |
synchronous() | |
print('Asynchronous:') | |
asynchronous() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment