python-flask-scoped_session创建session的两种方式,,scoped_ses


scoped_session        from sqlalchemy.orm import sessionmaker        from sqlalchemy import create_engine        from sqlalchemy.orm import scoped_session        engine = create_engine(                "mysql+pymysql://root:[email protected]:3306/s6?charset=utf8",                max_overflow=0,  # 超过连接池大小外最多创建的连接                pool_size=5,  # 连接池大小                pool_timeout=30,  # 池中没有线程最多等待的时间,否则报错                pool_recycle=-1  # 多久之后对线程池中的线程进行一次连接的回收(重置)        )        SessionFactory = sessionmaker(bind=engine)        # 方式一:由于无法提供线程共享功能,所有在开发时要注意,在每个线程中自己创建 session。        #         from sqlalchemy.orm.session import Session        #         自己具有操作数据库的:‘close‘, ‘commit‘, ‘connection‘, ‘delete‘, ‘execute‘, ‘expire‘,.....        session = SessionFactory()        # print(‘原生session‘,session)        # 操作        session.close()        # 方式二:支持线程安全,为每个线程创建一个session        #               实现1:- threading.Local        #               实现2:- 唯一标识        # ScopedSession对象        #       self.registry(), 加括号 创建session                #协程创建唯一标识        #from greenlet import getcurrent as get_ident        #session = scoped_session(SessionFactory,get_ident)                #本地线程创建唯一标识        session = scoped_session(SessionFactory)        # 操作        session.remove()

python-flask-scoped_session创建session的两种方式

评论关闭