How does async work in Tornado?

笑着哭i 提交于 2019-12-08 05:22:38

问题


All examples in the Tornado documents show how we can make further HTTP requests asynchronously using Tornado.

http_client = httpclient.AsyncHTTPClient()
http_client.fetch("http://www.google.com/", handle_request)

While I want to ensure that my tornado doesn't wait for response from database (get command of couchbase) to send next.

Check my other bug to understand why I want async call to couchbase.


回答1:


As far as I know Couchbase driver is not an async driver so this wont work. Make the db call synchronous and optimize it to be as fast as possible.

The (scary) alternative is to take couchbase's driver and patch it with something like gevent to create your own async driver. I do not recommend doing this. If you really want to go down this route look at what this guy did with Motor - He made the mongo driver async.




回答2:


See the Tornado wiki on github and this similar question on StackOverflow. Just do your database calls sync. If your database or your calls to the database/the database driver is your bottleneck your website will not be faster even if tornado is not blocked. Doing asynchronous calls may be more senseful when requesting ressources not under your control as other webservers or ressources which are not vital to most parts of your website like file I/Os.




回答3:


Old thread, but I should probably mention that couchbase has support for twisted. See Twisted API for Couchbase not working with Python Tornado and http://pythonhosted.org/couchbase/api/txcouchbase.html



来源:https://stackoverflow.com/questions/15093487/how-does-async-work-in-tornado

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!