简体   繁体   English

异步在Tornado中如何工作?

[英]How does async work in Tornado?

All examples in the Tornado documents show how we can make further HTTP requests asynchronously using Tornado. Tornado文档中的所有示例都说明了如何使用Tornado异步发出更多HTTP请求。

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. 检查我的其他错误,以了解为什么要异步调用Couchbase。

As far as I know Couchbase driver is not an async driver so this wont work. 据我所知,Couchbase驱动程序不是异步驱动程序,所以这将无法工作。 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. (吓人的)替代方法是使用cubbase的驱动程序,并使用gevent之类的补丁对其进行修补,以创建自己的异步驱动程序。 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. 如果您真的想走这条路,请看看这个家伙对Motor所做的事情-他使mongo驱动程序异步。

See the Tornado wiki on github and this similar question on StackOverflow . 请参阅github上Tornado WikiStackOverflow上的类似问题 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. 当请求资源不受其他Web服务器或资源(对于文件I / O而言对您的网站的大多数部分而言都不重要)的其他Web服务器或资源控制时,进行异步调用可能会更有意义。

Old thread, but I should probably mention that couchbase has support for twisted . 旧线程,但我可能应该提到沙发床支持twisted See Twisted API for Couchbase not working with Python Tornado and http://pythonhosted.org/couchbase/api/txcouchbase.html 请参阅Twisted API for Couchbase与Python Tornado不兼容,以及http://pythonhosted.org/couchbase/api/txcouchbase.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM