gpt4 book ai didi

python - 如何在 Tornado 中为具有高度可扩展基础架构的应用程序调用 MySQL 数据库,从而进行大量数据库查询?

转载 作者:行者123 更新时间:2023-11-28 22:39:22 32 4
gpt4 key购买 nike

那么,对于具有高度可扩展基础设施并进行大量数据库查询的高性能应用程序,在 Tornado 中进行数据库调用的最佳方法是什么?

方法 1:所以我遇到了异步数据库驱动程序/客户端,如 TorMySQL、Tornado-Mysql、asynctorndb 等,它们可以执行异步数据库调用。

方法二:使用通用的MySQLdb或mysqlclient(by PyMySQL)驱动程序,进行数据库调用,分离后端服务,并用nginx对调用进行负载均衡。类似于他们在其中一个小组中提到的原始 Friendfeed 人员所做的事情,

原作者之一布雷特·泰勒 (Bret Taylor) 写道:

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),我们可以

    执行器 = ThreadPoolExecutor(4)

    result = yield executor.submit(mysqldb_operation)

方法 4:只需使用 MySQLdb 等同步驱动程序并启动足够的 Tornado 实例并使用 nginx 进行负载平衡,应用程序在更广泛的层面上保持异步,一些调用被阻止,但其他请求有益于异步性质通过大量 Tornado 实例。

“解释”:

有关详细信息,请访问此链接 - www.jjinux.com/2009/12/python-asynchronous-networking-apis-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.

最佳答案

FriendFeed 使用了您所谓的“方法 4”:没有单独的后端服务;该进程刚刚在数据库调用期间被阻止。

通常最好使用完全异步的驱动程序(“方法 1”)。但是,这意味着您不能使用像 SQLAlchemy 这样包装数据库操作的同步库。在这种情况下,您可能必须使用线程(“方法 3”),这几乎和“方法 2”一样好(并且比“方法 2”更容易)。

“方法4”的优点是简单。过去这足以推荐它,因为到处引入回调很乏味;随着协同程序的出现,方法 3 几乎同样简单,因此通常使用线程比阻塞进程更好。

关于python - 如何在 Tornado 中为具有高度可扩展基础架构的应用程序调用 MySQL 数据库,从而进行大量数据库查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34799594/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com