V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
p8p8
V2EX  ›  Python

各位大仙们,看看下面的这段代码是否是 tornado 异步的代码?

  •  1
     
  •   p8p8 · 2014-11-26 10:08:36 +08:00 · 2909 次点击
    这是一个创建于 3431 天前的主题,其中的信息可能已经有所发展或是发生改变。
    #!/usr/bin/env python3
    
    import os
    import urllib.parse
    
    import tornado.gen
    import tornado.escape
    import tornado.httpclient
    import tornado.ioloop
    import tornado.web
    from base import BaseHandler
    
    from config import web as config
    import db
    import rs
    import qntoken
    from auth import auth_required
    
    from lib.utils import Validators
    import random
    from lib.desdec import *
    
    class MainHandler(BaseHandler):
        #@auth_required
        @tornado.gen.coroutine
        def get(self):
            user_rows = yield self.db.get_userlist()
            self.set_header('Content-Type', 'application/json')
            self.write(tornado.escape.json_encode(user_rows))
    

    这样的代码是tornado异步成功了吗?

    8 条回复    2014-11-27 16:29:33 +08:00
    fasling
        1
    fasling  
       2014-11-26 10:23:41 +08:00
    daniel7725
        2
    daniel7725  
       2014-11-26 10:32:22 +08:00
    这要看你的"db.get_userlist()"是不是支持异步的。如果它只是阻塞的 那么这么写是木有用的。
    geew
        3
    geew  
       2014-11-26 10:44:10 +08:00
    以前发过一个讨论的 你可以看看
    http://www.v2ex.com/t/88797
    然后有人提到了用celery和线程池, 有些资料可以参考下:
    http://segmentfault.com/blog/houxiyang/1190000000729575
    http://segmentfault.com/q/1010000000759709
    zenliver
        4
    zenliver  
       2014-11-26 13:29:33 +08:00
    @geew https://github.com/mayflaver/tornado-celery 简单易懂, 一看就明白
    p8p8
        5
    p8p8  
    OP
       2014-11-26 15:07:09 +08:00
    @daniel7725 数据用的是postgresql 异步的方法,应该就是了?
    tonghuashuai
        6
    tonghuashuai  
       2014-11-26 18:14:52 +08:00
    class GenAsyncHandler(RequestHandler):
    @gen.coroutine
    def get(self):
    http_client = AsyncHTTPClient()
    response = yield http_client.fetch("http://example.com")
    do_something_with_response(response)
    self.render("template.html")
    tonghuashuai
        7
    tonghuashuai  
       2014-11-26 18:15:37 +08:00
    刚发的全乱了,看官方文档吧: http://tornado.readthedocs.org/en/latest/gen.html
    p8p8
        8
    p8p8  
    OP
       2014-11-27 16:29:33 +08:00
    @tonghuashuai 嗯,谢谢你,我后来明白了,我的那段代码,就是异步的,关键在于数据库那个部分,而postgresql我也已经用异步处理了,所以整个代码是异步执行的。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1015 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 105ms · UTC 19:36 · PVG 03:36 · LAX 12:36 · JFK 15:36
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.