How mysqlclient and pymysql choose? The use _gevent_waiter

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/u011510825/article/details/86632598

    Connection using python mysql, is the need for tripartite package, the current mainstream way is pymysql and mysqlclient (ie Python3 version of MySQLdb). Another cymysql (optional with the fork of pymysql speedups C)

 

1. The author is the same person two libraries INADA Naoki, pip library mailboxes are pointing to mailto: [email protected]

2. PyMySQL code staff methane say mysqlclient faster and PyMySQL application scenarios

 

    So, how should we choose? First, we need to know about these two packages.

    A, pymysql

      1) to achieve pure Python, simple installation (directly mounted pip)

      2) Due to the pure Python implementation can be a good combination with gevent framework

 

Two, mysqlclient

    1) is a C extension module, the compiler installation may cause a variety of errors reported, obviously no easy pymysql

    2) speed;

 

The above comparison, you should be clear, is the ratio of performance pymysql mysqlclient poor, but pymysql simple and easy to use. Generally you have to choose mysqlclient unless the following three conditions:

1) You can not use libmysqlclient for some reason

2) you want to use with gevent or eventlet

3) Consider protocol compatibility mysql

The original is this:

mysqlclient-python is much faster than PyMySQL.
When to use PyMySQL is:

You can't use libmysqlclient for some reason
You want to use monkeypatched socket of gevent or eventlet
You wan't to hack mysql protocol

For the above reasons, led to the current use pymysql programmers far more than mysqlclient, especially the second, and now the python site, to be used for basic gevent or eventlet it.

Powerful mysqlclient has been resolved, mysqlclient gevent the current can also be used. Directly on the code:

import MySQLdb
import gevent.hub

def _gevent_waiter(fd, hub=gevent.hub.get_hub()):
    hub.wait(hub.loop.io(fd, 1))




conn = MySQLdb.connect(
                db=self.db,
                host=self.host if not self.ssh else LOOPBACK_ADDRESS,
                port=self.port if not self.ssh else int(self.ssh.local_bind_port),
                user=self.user,
                password=self.passwd,
                charset=self.charset,
                connect_timeout=self.connect_timeout,
                cursorclass=DictCursor,
                use_unicode=True,
                waiter=_gevent_waiter,
            )


The above connection, other parameters much explanation, it said waiter, with the equivalent mysqlclinet seamlessly with the gevent.

Do not understand that _gevent_waiter, you can read this article   gevent Hub  .

So, how do we test it? The testing process is relatively simple, from a single web service process, when the connection to the database when the query is executed time.sleep 20 seconds database, this time to see other requests the web service can not process the request, with the perfect waiter = _gevent_waiter what. . .

With this, the system has a large number of database query services, I may never use a tornado. .

 

github Address:

https://github.com/PyMySQL/mysqlclient-python

https://github.com/PyMySQL/PyMySQL/

 

At present the indicators of contrast pymysql and mysqlclient:

https://python.libhunt.com/compare-mysqlclient-python-vs-pymysql

Most of the program is chilling ah ,,, ape is still relatively trouble, choose an easy to use but the poor performance of pymysql ...... pymysql MySQLdb slower than too much, so big projects still feel a bit tasteless ah pymysql in this suggested that the use of MySQLdb.

 

 

Guess you like

Origin blog.csdn.net/u011510825/article/details/86632598