那么,对于一个高性能的应用程序,在Tornado中进行数据库调用,哪一种方法更好?该应用程序具有高度可扩展的基础结构,可以进行大量的数据库查询?在
方法1:因此,我遇到过一些异步数据库驱动程序/客户机,如TorMySQL、Tornado Mysql、asynctorndb等,它们可以执行异步数据库调用。在
方法2:使用通用和通用的MySQLdb或mysqlclient(by PyMySQL)驱动程序,调用数据库以分离后端服务,并使用nginx进行负载平衡。
就像他们在其中一个小组里提到的,Friendfeed最初的做法一样
原著作者之一布雷特·泰勒写道:groups.google.com/group/python-tornado/browse_thread/thread/9a08f5f08cdab108
We experimented with different async DB approaches, but settled on
synchronous at FriendFeed because generally if our DB queries were
backlogging our requests, our backends couldn't scale to the load
anyway. Things that were slow enough were abstracted to separate
backend services which we fetched asynchronously via the async HTTP
module.
方法2的其他支持链接可以更好地理解我要说的内容:
方法3:通过使用线程和IOLoop并使用通用同步库。在
支持例子链接来解释我的意思Do it synchronously and block the IOLoop. This is most appropriate for
things like memcache and database queries that are under your control
and should always be fast. If it's not fast, make it fast by adding
the appropriate indexes to the database, etc.另一种示例方法:
对于同步数据库(mysqldb),我们可以
执行器=线程池执行器(4)
结果=产量执行人.提交(mysqldb_操作)
方法4:只需使用MySQLdb等同步驱动程序,并使用nginx启动足够的Tornado实例和负载平衡,使应用程序在更广泛的层面上保持异步,一些调用被阻止,但其他请求通过大量Tornado实例受益于异步性质。在
“解释”:
有关详细信息,请访问此链接-www.jjinux.com/2009/12/python-asynchronous-networking-api-and.html,上面写着:They allow MySQL queries to block the entire process. However, they
compensate in two ways. They lean heavily on their asynchronous web
client wherever possible. They also make use of multiple Python
processes. Hence, if you're handling 500 simultaneous request, you
might use nginx to split them among 10 different Tornado Web
processes. Each process is handling 50 simultaneous requests. If one
of those requests needs to make a call to the database, only 50
(instead of 500) requests are blocked.