小编J. *_*zyk的帖子

使用Flask的SQLAlchemy会话引发"在线程中创建的SQLite对象只能在同一个线程中使用"

我有一个Flask视图,它使用SQLAlchemy查询和显示一些博客文章.我正在使用mod_wsgi运行我的应用程序.此视图在我第一次进入页面时起作用,但下次返回500错误.回溯显示错误ProgrammingError: SQLite objects created in a thread can only be used in that same thread. 为什么我收到此错误以及如何解决?

views.py

engine = create_engine('sqlite:////var/www/homepage/blog.db')
Base.metadata.bind = engine
DBSession = sessionmaker(bind = engine)
session = DBSession()

@app.route('/blog')
@app.route('/blog.html')
def blog():
    entrys = session.query(Entry).order_by(desc(Entry.timestamp)).all()
    return render_template('blog.html', blog_entrys = entrys)
Run Code Online (Sandbox Code Playgroud)

models.py:

class Entry(Base):
    __tablename__ = 'entry'

    id = Column(Integer, primary_key = True)

    title = Column(String(100), nullable = False)
    body = Column(String, nullable = False)
    timestamp = Column(DateTime, nullable = False)
    featured = Column(Boolean, nullable = …
Run Code Online (Sandbox Code Playgroud)

python sqlite sqlalchemy flask

19
推荐指数
2
解决办法
1万
查看次数

标签 统计

flask ×1

python ×1

sqlalchemy ×1

sqlite ×1