Connection pooling with Python + MySQL

How to do connection pooling with MySQL in Python and what to do with it.

mysql-connector-python MySQLConnectionPool issue

First, if you simply want to create a connection with the official MySQL Python client mysql-connector-python, you can write:

import mysql.connector

cnx = mysql.connector.connect(host="...", user="...", password="...", database="...")

This mysql-connector-python provides a class called MySQLConnectionPool, which makes it easy to do connection pooling. [^ MySQLConnectionPool]

from mysql.connector.pooling import MySQLConnectionPool

cnxpool = MySQLConnectionPool(host="...", user="...", password="...", database="...", pool_size=10)

cnx = cnxpool.get_connection()

cnx.close()  #The connection is not actually disconnected and is released into the connection pool

Congratulations ... ** is not the case. ** **

Try running the following code.

from mysql.connector.pooling import MySQLConnectionPool

cnxpool = MySQLConnectionPool(host="...", user="...", password="...", database="...", pool_size=3)

cnxpool.get_connection()
cnxpool.get_connection()
cnxpool.get_connection()
cnxpool.get_connection()  #=> PoolError: Failed getting connection; pool exhausted

MySQLConnectionPool is designed to ** throw an exception when the pool connections are exhausted **.

I don't hate this simple specification, but when actually using it in a Web application, it is often nice to have the behavior of "waiting for another connection to be released". Otherwise, an error will occur even if the number of parallel requests processed exceeds the pool size even a little.

SQLAlchemy QueuePool Solution

This problem can be solved by using SQLAlchemy's QueuePool. [^ Queue Pool]

from sqlalchemy.pool import QueuePool

cnxpool = QueuePool(lambda: mysql.connector.connect(host="...", user="...", password="...", database="..."), pool_size=10)

cnx = cnxpool.connect()

It seems that the first argument of QueuePool can be any function that returns "connection object corresponding to DB API". It's versatile and amazing.

The module itself called SQLAlchemy is an ORM, but this time we are using only the mechanism called QueuePool without using the ORM part at all.

Recommended Posts

Connection pooling with Python + MySQL
Debug for mysql connection with python mysql.connector
UDP simultaneous connection with Python
Build Mysql + Python environment with docker
FizzBuzz with Python3
Receive textual data from mysql with python
Scraping with Python
INSERT into MySQL with Python [For beginners]
Scraping with Python
Python with Go
Image processing with Python 100 knocks # 8 Max pooling
Twilio with Python
Integrate with Python
Connect to MySQL with Python within Docker
Play with 2016-Python
AES256 with python
Tested with Python
python starts with ()
with syntax (Python)
Bingo with python
Zundokokiyoshi with python
Let's do MySQL data manipulation with Python
Image processing with Python 100 knocks # 7 Average pooling
Excel with Python
Microcomputer with Python
Cast with python
ODBC connection to FileMaker 11 Server Advanced with Python 3
Connect to MySQL with Python on Raspberry Pi
[Introduction for beginners] Working with MySQL in Python
Serial communication with Python
Zip, unzip with python
Django 1.11 started with Python3.6
Python with eclipse + PyDev.
Socket communication with Python
Data analysis with python 2
Scraping with Python (preparation)
Touch MySQL from Python 3
Try scraping with Python.
Learning Python with ChemTHEATER 03
Sequential search with Python
"Object-oriented" learning with python
Handling yaml with python
Solve AtCoder 167 with python
Serial communication with python
[Python] Use JSON with Python
Learning Python with ChemTHEATER 05-1
Learn Python with ChemTHEATER
Run prepDE.py with python3
1.1 Getting Started with Python
Collecting tweets with Python
Binarization with OpenCV / Python
3. 3. AI programming with Python
Kernel Method with Python
Scraping with Python + PhantomJS
Use MySQL from Python
Drive WebDriver with python
[Python] Redirect with CGIHTTPServer
Voice analysis with python
Think yaml with python
Operate Kinesis with Python
Use DynamoDB with Python