各位大仙们,看看下面的这段代码是否是 tornado 异步的代码? - V2EX
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 3513 次点击
    这是一个创建于 3984 天前的主题,其中的信息可能已经有所发展或是发生改变。
    #!/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     Solana     1510 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 28ms UTC 16:21 PVG 00:21 LAX 09:21 JFK 12:21
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86