V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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 · Nov 26, 2014 · 3891 views
    This topic created in 4177 days ago, the information mentioned may be changed or developed.
    #!/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 replies    2014-11-27 16:29:33 +08:00
    fasling
        1
    fasling  
       Nov 26, 2014
    daniel7725
        2
    daniel7725  
       Nov 26, 2014
    这要看你的"db.get_userlist()"是不是支持异步的。如果它只是阻塞的 那么这么写是木有用的。
    geew
        3
    geew  
       Nov 26, 2014
    以前发过一个讨论的 你可以看看
    https://study.congcong.us/t/88797
    然后有人提到了用celery和线程池, 有些资料可以参考下:
    http://segmentfault.com/blog/houxiyang/1190000000729575
    http://segmentfault.com/q/1010000000759709
    zenliver
        4
    zenliver  
       Nov 26, 2014
    @geew https://github.com/mayflaver/tornado-celery 简单易懂, 一看就明白
    p8p8
        5
    p8p8  
    OP
       Nov 26, 2014
    @daniel7725 数据用的是postgresql 异步的方法,应该就是了?
    tonghuashuai
        6
    tonghuashuai  
       Nov 26, 2014
    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  
       Nov 26, 2014
    刚发的全乱了,看官方文档吧: http://tornado.readthedocs.org/en/latest/gen.html
    p8p8
        8
    p8p8  
    OP
       Nov 27, 2014
    @tonghuashuai 嗯,谢谢你,我后来明白了,我的那段代码,就是异步的,关键在于数据库那个部分,而postgresql我也已经用异步处理了,所以整个代码是异步执行的。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   819 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 50ms · UTC 22:37 · PVG 06:37 · LAX 15:37 · JFK 18:37
    ♥ Do have faith in what you're doing.