Commit 61c768b4 authored by Denis Bilenko's avatar Denis Bilenko

examples: follow pep8

parent 1bb128b8
...@@ -13,6 +13,7 @@ monkey.patch_all() ...@@ -13,6 +13,7 @@ monkey.patch_all()
import urllib2 import urllib2
def print_head(url): def print_head(url):
print 'Starting %s' % url print 'Starting %s' % url
data = urllib2.urlopen(url).read() data = urllib2.urlopen(url).read()
...@@ -21,4 +22,3 @@ def print_head(url): ...@@ -21,4 +22,3 @@ def print_head(url):
jobs = [gevent.spawn(print_head, url) for url in urls] jobs = [gevent.spawn(print_head, url) for url in urls]
gevent.joinall(jobs) gevent.joinall(jobs)
...@@ -12,12 +12,11 @@ from gevent import socket ...@@ -12,12 +12,11 @@ from gevent import socket
from gevent.pool import Pool from gevent.pool import Pool
N = 1000 N = 1000
# limit ourselves to max 10 simultaneous outstanding requests # limit ourselves to max 10 simultaneous outstanding requests
pool = Pool(10) pool = Pool(10)
finished = 0 finished = 0
def job(url): def job(url):
global finished global finished
try: try:
......
...@@ -6,6 +6,7 @@ WSGI interface is a safer choice, see examples/wsgiserver.py. ...@@ -6,6 +6,7 @@ WSGI interface is a safer choice, see examples/wsgiserver.py.
""" """
from gevent import http from gevent import http
def callback(request): def callback(request):
print request print request
if request.uri == '/': if request.uri == '/':
......
...@@ -12,14 +12,15 @@ from gevent import socket ...@@ -12,14 +12,15 @@ from gevent import socket
import subprocess import subprocess
import errno import errno
import sys import sys
import fcntl, os import os
import fcntl
def popen_communicate(args, data=''): def popen_communicate(args, data=''):
"""Communicate with the process non-blockingly.""" """Communicate with the process non-blockingly."""
p = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) p = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
fcntl.fcntl(p.stdin, fcntl.F_SETFL, os.O_NONBLOCK) # make the file nonblocking fcntl.fcntl(p.stdin, fcntl.F_SETFL, os.O_NONBLOCK) # make the file nonblocking
fcntl.fcntl(p.stdout, fcntl.F_SETFL, os.O_NONBLOCK) # make the file nonblocking fcntl.fcntl(p.stdout, fcntl.F_SETFL, os.O_NONBLOCK) # make the file nonblocking
bytes_total = len(data) bytes_total = len(data)
bytes_written = 0 bytes_written = 0
......
#!/usr/bin/python #!/usr/bin/python
"""A web.py application powered by gevent""" """A web.py application powered by gevent"""
from gevent import monkey; monkey.patch_all() from gevent import monkey; monkey.patch_all()
...@@ -10,10 +9,12 @@ import web ...@@ -10,10 +9,12 @@ import web
urls = ("/", "index", urls = ("/", "index",
'/long', 'long_polling') '/long', 'long_polling')
class index: class index:
def GET(self): def GET(self):
return 'Hello, world!<br><a href="/long">/long</a>' return 'Hello, world!<br><a href="/long">/long</a>'
class long_polling: class long_polling:
# Since gevent.wsgi executes each incoming connection in a separate greenlet # Since gevent.wsgi executes each incoming connection in a separate greenlet
# long running requests such as this one don't block one another; # long running requests such as this one don't block one another;
...@@ -21,11 +22,10 @@ class long_polling: ...@@ -21,11 +22,10 @@ class long_polling:
# becomes greenlet-local storage thus making requests isolated as they should be. # becomes greenlet-local storage thus making requests isolated as they should be.
def GET(self): def GET(self):
print 'handling GET context id = %s' % (id(web.ctx._getd()), ) print 'handling GET context id = %s' % (id(web.ctx._getd()), )
gevent.sleep(10) # possible to block the request indefinitely, without harming others gevent.sleep(10) # possible to block the request indefinitely, without harming others
return 'Hello, 10 seconds later' return 'Hello, 10 seconds later'
if __name__ == "__main__": if __name__ == "__main__":
application = web.application(urls, globals()).wsgifunc() application = web.application(urls, globals()).wsgifunc()
print 'Serving on 8088...' print 'Serving on 8088...'
WSGIServer(('', 8088), application).serve_forever() WSGIServer(('', 8088), application).serve_forever()
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
from gevent import wsgi from gevent import wsgi
def hello_world(env, start_response): def hello_world(env, start_response):
if env['PATH_INFO'] == '/': if env['PATH_INFO'] == '/':
start_response('200 OK', [('Content-Type', 'text/html')]) start_response('200 OK', [('Content-Type', 'text/html')])
...@@ -13,4 +14,3 @@ def hello_world(env, start_response): ...@@ -13,4 +14,3 @@ def hello_world(env, start_response):
print 'Serving on 8088...' print 'Serving on 8088...'
wsgi.WSGIServer(('', 8088), hello_world).serve_forever() wsgi.WSGIServer(('', 8088), hello_world).serve_forever()
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
from gevent import pywsgi from gevent import pywsgi
def hello_world(env, start_response): def hello_world(env, start_response):
if env['PATH_INFO'] == '/': if env['PATH_INFO'] == '/':
start_response('200 OK', [('Content-Type', 'text/html')]) start_response('200 OK', [('Content-Type', 'text/html')])
...@@ -16,4 +17,3 @@ server = pywsgi.WSGIServer(('0.0.0.0', 8443), hello_world, keyfile='server.key', ...@@ -16,4 +17,3 @@ server = pywsgi.WSGIServer(('0.0.0.0', 8443), hello_world, keyfile='server.key',
# to start the server asynchronously, call server.start() # to start the server asynchronously, call server.start()
# we use blocking serve_forever() here because we have no other jobs # we use blocking serve_forever() here because we have no other jobs
server.serve_forever() server.serve_forever()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment