我有一个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)