Python - peewee 数据库重连,连接池连接不够
Max.Bai
2019-05
peewee 是轻量级的python ORM工具。
官网的demo:
from peewee import *
settings = {'host': 'localhost', 'password': '', 'port': 3306, 'user': 'root'}
db = MySQLDatabase("test",**settings)
class Person(Model):
name = CharField()
birthday = DateField()
class Meta:
database = db
这样基本是可以使用的
使用前要先connect db,使用完要记得close。
如果遇到多线程。。。操作就有点麻烦,有没有自动重连的机制,当然有
自己写一个类继承from playhouse.shortcuts import ReconnectMixin 就可以了
问题又来了,长时间执行连接会超时断开
这个时候就想到了连接池。
当然还是官网的demo了
from playhouse.pool import PooledPostgresqlExtDatabase
db = PooledPostgresqlExtDatabase(
'my_app',
max_connections=32,
stale_timeout=300, # 5 minutes.
user='postgres')
class BaseModel(Model):
class Meta:
database = db
我想又使用