LoginSignup
0
0

More than 1 year has passed since last update.

MySQL スレッドプール

Last updated at Posted at 2021-12-07

MySQL スレッドプール サンプルモジュール(Python)

参考:https://dev.mysql.com/doc/refman/5.6/ja/faqs-thread-pool.html#faq-thread-pool-what-solve
個人ブログ:https://wintercat.jp/post/databasecat/
あくまでもご参考で〜:coffee:

import 必要なモジュール

import pymysql
from dbutils.pooled_db import PooledDB
import config
# -*- coding:utf-8 -*-
# @Author: WinterCat
# @File : database_pool.py

class DataBaseConnectionPool:
    __pool = None
    # init database connection
    def __init__(self):
        self.conn = self.get_connection().connection()
        self.cur = self.conn.cursor(pymysql.cursors.DictCursor)

    # connection create and create config file to set it
    def get_connection(self):
        if self.__pool is None:
            __pool = PooledDB(
                creator=pymysql,
                maxconnections=config.DB_MAX_CONNECTIONS,
                mincached=config.DB_MIN_CACHED,
                maxcached=config.DB_MAX_CACHED,
                maxshared=config.DB_MAX_SHARED,
                blocking=config.DB_BLOCKING,
                maxusage=config.DB_MAX_USAGE,
                setsession=config.DB_SET_SESSION,
                ping=config.DB_PING,
                host=config.DB_HOST,
                user=config.DB_USER,
                password=config.DB_PASSWORD,
                database=config.DB_NAME,
                charset=config.DB_CHARSET
            )
        return __pool

    # connection auto close
    def __del__(self):
        self.cur.close()
        self.conn.close()


# search database by query
def search_data(query):
    connection = DataBaseConnectionPool()
    connection.cur.execute(query)
    return connection.cur.fetchall()


# insert data by query
def update_data(query):
    connection = DataBaseConnectionPool()
    connection.cur.execute(query)
    return connection.conn.commit()

config.py

基本的に設置または編集が必要なところは
DB_HOST, DB_POST, DB_USER, DB_PASSWORD, DB_NAME となります。

DB_CREATOR = 'pymysql'
DB_HOST = 'IP ADDRESS'  # データベースのIPアドレス
DB_PORT = 3306
DB_NAME = 'DB_NAME' # データベース名前
DB_USER = 'USERNAME' 
DB_PASSWORD = 'PASSWORD'
DB_CHARSET = 'utf8mb4' # encode
# free cached min
DB_MIN_CACHED = 10
# free cached max
DB_MAX_CACHED = 22
#  pools create max
DB_MAX_CONNECTIONS = 100
#  share pool max
DB_MAX_SHARED = 20
# max pool block
DB_BLOCKING = True
#  connection use again
DB_MAX_USAGE = 0
# session
DB_SET_SESSION = []
# ping
DB_PING = 0

使用方法について

検索の方法は:search_data を使え
何かをInsert、またはアップデートすると:add_data を使え
↓例となります。

# @Author: WinterCat
from database_service.database_pool import add_data, search_data
# テーブルを作る
def create_table():
    sql = '''
    CREATE TABLE IF NOT EXISTS sys(ID INT PRIMARY KEY AUTO_INCREMENT,message VARCHAR(50))
    '''
    return add_data(query=sql)

if __name__ == '__main__':
    print(create_table())
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0