sqlalchemy的连接方式
这是比较推荐的连接方式, 基于threading.local实现的。
# -*- coding:utf-8 -*- from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker, scoped_session from sqlalchme_test import Users import threading engine = create_engine("sqlite:///test_s1.db") # 创建数据库连接 SessionFactor =sessionmaker(bind=engine) # 为每个threading local放一个session session = scoped_session(SessionFactor) def task(): ret = session.query(Users).all() # 释放数据库连接 session.remove() for i in range(20): t = threading.Thread(args=task) t.start