Skip to content

Instantly share code, notes, and snippets.

@nkelner
Created October 9, 2013 04:30
Show Gist options
  • Save nkelner/6896241 to your computer and use it in GitHub Desktop.
Save nkelner/6896241 to your computer and use it in GitHub Desktop.
Watch gevent pwn blocking api calls.
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