关于uwsgi+gevent+web.py的备忘

阅读更多
简单入门:
uwsgi:nginx般的优雅控制
gevent:高效、无缝的接入
web.py:简洁

关于uwsgi+web.py:
import web

urls = (
    '/(.*)', 'hello'
)
app = web.application(urls, globals())

class hello:
    def GET(self, name):
        if not name:
            name = 'World'
        return 'Hello, ' + name + '!'

application = app.wsgifunc()

执行:
./uwsgi -s /tmp/web.py.socket -w testapp


关于uwsgi+gevent:
import gevent
import gevent.socket

def bg_task():
    for i in range(1,10):
        print "background task", i
        gevent.sleep(2)

def long_task():
    for i in range(1,10):
        print i
        gevent.sleep()

def application(e, sr):

    sr('200 OK', [('Content-Type','text/html')])

    t = gevent.spawn(long_task)

    t.join()

    yield "sleeping for 3 seconds...
" gevent.sleep(3) yield "done
" yield "getting some ips...
" urls = ['www.google.com', 'www.example.com', 'www.python.org', 'projects.unbit.it'] jobs = [gevent.spawn(gevent.socket.gethostbyname, url) for url in urls] gevent.joinall(jobs, timeout=2) for j in jobs: yield "ip = %s
" % j.value # this task will goes on after request end gevent.spawn(bg_task)

执行:
uwsgi --loop gevent --socket :3031 --module myapp --async 100


关于gevent+web.py:
from gevent import monkey; monkey.patch_all()
from gevent.wsgi import WSGIServer
import gevent
import web

urls = ("/", "index",
        '/long', 'long_polling')


class index:
    def GET(self):
        return 'Hello, world!
[url=/long]/long[/url]' class long_polling: def GET(self): print 'handling GET context id = %s' % (id(web.ctx._getd()), ) gevent.sleep(10) # possible to block the request indefinitely, without harming others return 'Hello, 10 seconds later' if __name__ == "__main__": application = web.application(urls, globals()).wsgifunc() print 'Serving on 8088...' WSGIServer(('', 8088), application).serve_forever()

执行:
python model.py


三合一:
from gevent import monkey; monkey.patch_all()
from gevent.wsgi import WSGIServer
import gevent
import web

urls = ("/", "index",
        '/long', 'long_polling')


class index:
    def GET(self):
        return 'Hello, world!
[url=/long]/long[/url]' class long_polling: def GET(self): print 'handling GET context id = %s' % (id(web.ctx._getd()), ) gevent.sleep(10) # possible to block the request indefinitely, without harming others return 'Hello, 10 seconds later' application = web.application(urls, globals()).wsgifunc() #print 'Serving on 8088...' #WSGIServer(('', 8088), application).serve_forever() #注释掉,就这么简单

执行:
uwsgi --loop gevent --socket :3031 --module myapp --async 100


注意:
uwsgi只支持gevent1.0以上版本,而gevent目前的stable还没有升上去,要尝新的话得要担风险。

另,我在编译uwsgi+gevent plugin时出了点问题:
引用

cc1: warnings being treated as errors
> plugins/gevent/gevent.c: In function ‘py_uwsgi_gevent_request’:
> plugins/gevent/gevent.c:190: warning: suggest explicit braces to avoid
> ambiguous ‘else’
> plugins/gevent/gevent.c:190: warning: suggest explicit braces to avoid
> ambiguous ‘else’
> plugins/gevent/gevent.c:199: warning: suggest explicit braces to avoid
> ambiguous ‘else’
> plugins/gevent/gevent.c:199: warning: suggest explicit braces to avoid
> ambiguous ‘else’
> plugins/gevent/gevent.c:216: warning: suggest explicit braces to avoid
> ambiguous ‘else’
> plugins/gevent/gevent.c:216: warning: suggest explicit braces to avoid
> ambiguous ‘else’
> make: *** [all] Error 1

这可能是gcc版本有点旧,可以用这个补丁: http://projects.unbit.it/hg/uwsgi-1.0/rev/9943f97612f2(感谢Roberto的帮助)

留此备忘,静待稳定版

你可能感兴趣的:(python,web)