这是比较推荐的连接方式, 基于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
标签:engine,sqlalchemy,方式,threading,session,import,连接 From: https://www.cnblogs.com/kongzhagen/p/16886984.html