本文介绍了我怎么能在龙卷风使用Python3.4进行异步操作的MySQL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在用Python3.4,我想在龙卷风使用异步MySQL客户端。我发现但阅读它的源$ C ​​$ C后,我认为它不能进行异步的MySQL操作,因为它只是封装MySQLdb的包。

I now use Python3.4 and I want to use asynchronous mysql client in Tornado. I have found torndb but after reading its source code, I think it couldn't make asynchronous mysql operations because it just encapsulates MySQLdb package.

那么,有没有办法在龙卷风进行异步操作的mysql?

So is there a way to make asynchronous mysql operations in Tornado?

推荐答案

使用MySQL与龙卷风的正规途径是使用一套独立的流程跟MySQL和使用异步HTTP请求去跟那些服务器(也见答案#2 )。这些过程可以是相同的机器和使用龙卷风,或其他地方的应用程序服务器上。一个最小的例子:

The canonical way to use MySQL with tornado is to use a separate set of processes to talk to MySQL and use asynchronous http requests to talk to those servers (see also answer #2 in Is Tornado really non-blocking?). These processes can be on the same machine and using tornado, or application servers somewhere else. A minimal example:

import json, sys, time
from MySQLdb import connect, cursors
from tornado import gen, httpclient, web, netutil, process, httpserver, ioloop

class BackendHandler(web.RequestHandler):
    def get(self):
        time.sleep(1)  # simulate longer query
        cur = connect(db='tornado', user='root').cursor(cursors.DictCursor)
        cur.execute("SELECT * FROM foo")
        self.write(json.dumps(list(cur.fetchall())))

class FrontendHandler(web.RequestHandler):
    @gen.coroutine
    def get(self):
        http_client = httpclient.AsyncHTTPClient(max_clients=500)
        response = yield http_client.fetch("http://localhost:8001/foo")
        self.set_header("Content-Type", 'application/json')
        self.write(response.body)


if __name__ == "__main__":
    number_of_be_tasks = int(sys.argv[1]) if len(sys.argv) > 1 else 20
    number_of_fe_tasks = int(sys.argv[2]) if len(sys.argv) > 2 else 1
    fe_sockets = netutil.bind_sockets(8000)  # need to bind sockets
    be_sockets = netutil.bind_sockets(8001)  # before forking
    task_id = process.fork_processes(number_of_be_tasks + number_of_fe_tasks)
    if task_id < number_of_fe_tasks:
        handler_class = FrontendHandler
        sockets = fe_sockets
    else:
        handler_class = BackendHandler
        sockets = be_sockets
    httpserver.HTTPServer(web.Application([(r"/foo", handler_class)])
        ).add_sockets(sockets)
    ioloop.IOLoop.instance().start()

也就是说,如果你的Web服务器正在做的是谈论到MySQL的主要事情龙卷风直接不赢你多少(只要你想并发MySQL连接,你需要尽可能多的进程)。在这种情况下一个更好的堆栈很可能是nginx的+ uwsgi +蟒蛇。什么龙卷风对正在与多个后端服务器,使用HTTP,潜在的并行真的很好。

That said, if the main thing your web server is doing is talking to MySQL directly tornado doesn't win you much (as you'll need as many processes as you want concurrent MySQL connections). In that case a better stack might well be nginx+uwsgi+python. What tornado's really good for is talking to multiple backend servers, using HTTP, potentially in parallel.

这篇关于我怎么能在龙卷风使用Python3.4进行异步操作的MySQL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 11:19