Home > Backend Development > Python Tutorial > How to use pymysqlpool in Python MySQL database?

How to use pymysqlpool in Python MySQL database?

零下一度
Release: 2017-07-09 11:57:26
Original
4121 people have browsed it

This article mainly introduces you to the relevant information about the Python MySQL database connection pool component pymysqlpool. The article introduces it in detail through the example code, which has certain reference and learning value for everyone. Friends in need Let’s take a look together below.

Introduction

pymysqlpool (local download) is a new member of the database toolkit, aiming to provide a practical database connection Pool middleware to avoid frequent creation and release of database connection resources in the application.

Function

  • The connection pool itself is It is thread-safe and can be used in a multi-threaded environment. There is no need to worry about connection resources being shared by multiple threads;

  • Provides the most compact interface possible for database operations;

  • The management of the connection pool is completed within the package, and the client can obtain the connection resources in the pool through the interface (return pymysql.Connection);

  • will be compatible with dataobj to the greatest extent and is easy to use;

  • The connection pool itself has the function of dynamically increasing the number of connections, that is, max_pool_size and step_size will Used to control the number of connections and the maximum number of connections added each time;

  • The maximum number of connections in the connection pool also increases dynamically. You need to turn on the enable_auto_resize switch. After that, when any connection acquisition timeout occurs, all It is recorded as a penalty, and max_pool_size is expanded by a certain multiple.

##Basic workflow

Note that when multiple threads request at the same time, if the pool If there is no available connection object, you need to wait in line.


|--------|        |--------------|
|  | <==borrow connection object== | Pool manager |
| Client |        |    |
|  | ==return connection object==> | FIFO queue |
|--------|        |--------------|
Copy after login

Parameter configuration

  • pool_name: The name of the connection pool, multiple connection parameters correspond to multiple different connection pool objects, multiple

    singleton mode;

  • host: database Address

  • user: Database server user name

  • password: User password

  • database: Default Selected database

  • port: The port of the database server

  • charset:

    Character set, the default is 'utf8'

  • use_dict_cursor: Use dictionary format or tuple to return data;

  • max_pool_size: Connection pool priority maximum number of connections;

  • step_size: The connection pool dynamically increases the number of connections;

  • enable_auto_resize: Whether to dynamically expand the connection pool, that is, when it exceeds max_pool_size, automatically expand max_pool_size;

  • pool_resize_boundary: This configuration is the upper limit that the connection pool can eventually increase, and the instantaneous expansion cannot exceed this value;

  • auto_resize_scale: Automatically expand the gain of max_pool_size, The default is 1.5 times expansion;

  • wait_timeout: How long to wait at most when queuing for a connection object. When timeout occurs, the connection pool attempts to automatically expand the current number of connections;

  • kwargs: Other configuration parameters will be passed to

    pymysql.Connection

# when creating the connection object ##Usage example
1. Use the cursor context manager (shortcut, but it will apply for a connection object every time it is obtained, so multiple calls are inefficient):


from pymysqlpool import ConnectionPool
config = {
 &#39;pool_name&#39;: &#39;test&#39;,
 &#39;host&#39;: &#39;localhost&#39;,
 &#39;port&#39;: 3306,
 &#39;user&#39;: &#39;root&#39;,
 &#39;password&#39;: &#39;root&#39;,
 &#39;database&#39;: &#39;test&#39;
}
def connection_pool():
 # Return a connection pool instance
 pool = ConnectionPool(**config)
 pool.connect()
 return pool
# 直接访问并获取一个 cursor 对象,自动 commit 模式会在这种方式下启用
with connection_pool().cursor() as cursor:
 print(&#39;Truncate table user&#39;)
 cursor.execute(&#39;TRUNCATE user&#39;)
 print(&#39;Insert one record&#39;)
 result = cursor.execute(&#39;INSERT INTO user (name, age) VALUES (%s, %s)&#39;, (&#39;Jerry&#39;, 20))
 print(result, cursor.lastrowid)
 print(&#39;Insert multiple records&#39;)
 users = [(name, age) for name in [&#39;Jacky&#39;, &#39;Mary&#39;, &#39;Micheal&#39;] for age in range(10, 15)]
 result = cursor.executemany(&#39;INSERT INTO user (name, age) VALUES (%s, %s)&#39;, users)
 print(result)
 print(&#39;View items in table user&#39;)
 cursor.execute(&#39;SELECT * FROM user&#39;)
 for user in cursor:
  print(user)
 print(&#39;Update the name of one user in the table&#39;)
 cursor.execute(&#39;UPDATE user SET name="Chris", age=29 WHERE id = 16&#39;)
 cursor.execute(&#39;SELECT * FROM user ORDER BY id DESC LIMIT 1&#39;)
 print(cursor.fetchone())
 print(&#39;Delete the last record&#39;)
 cursor.execute(&#39;DELETE FROM user WHERE id = 16&#39;)
Copy after login

2. Use connection context manager:


import pandas as pd
from pymysqlpool import ConnectionPool
config = {
 &#39;pool_name&#39;: &#39;test&#39;,
 &#39;host&#39;: &#39;localhost&#39;,
 &#39;port&#39;: 3306,
 &#39;user&#39;: &#39;root&#39;,
 &#39;password&#39;: &#39;root&#39;,
 &#39;database&#39;: &#39;test&#39;
}
def connection_pool():
 # Return a connection pool instance
 pool = ConnectionPool(**config)
 pool.connect()
 return pool
with connection_pool().connection() as conn:
 pd.read_sql(&#39;SELECT * FROM user&#39;, conn)
# 或者
connection = connection_pool().borrow_connection()
pd.read_sql(&#39;SELECT * FROM user&#39;, conn)
connection_pool().return_connection(connection)
Copy after login

Please test more Move to test_example.py.

Dependency

    pymysql: You will rely on this tool package to complete database connection and other operations;
  • pandas: pandas was used during testing.

The above is the detailed content of How to use pymysqlpool in Python MySQL database?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template