这是我网站上的相关代码__init__.py
from site.models import initialise_sql
def main(global_config, **settings):
""" This function returns a Pyramid WSGI application """
initialise_sql(settings['database.url'])
return config.make_wsgi_app()
这是我的models/__init__.py
from storm.locals import *
from datetime import datetime
DBSession = None
class Tables:
""" A simple namespace for all the table objects in our database """
class Users(object):
__storm_table__ = 'users'
user_id = Int(primary=True)
username = RawStr()
password = RawStr()
date_created = DateTime(default=datetime.now())
def initialise_sql(db_uri):
""" Initialise the database """
global DBSession
database = create_database(db_uri)
DBSession = Store(database)
这是我的用户模型:
def create_account(username, password):
user = Tables.Users()
user.username = username
user.password = password
DBSession.add(user)
DBSession.flush()
根据我对 Storm 文档的阅读,这一切都是正确的。问题是,当create_account
从我的视图可调用函数中调用该函数时,我会抛出以下异常:
ProgrammingError: SQLite objects created in a thread can only be used in that same thread.The object was created in thread id -1220417856 and this is thread id -1239418000
我什至不知道 Pyramid 正在线程化应用程序:/
我怎样才能解决这个问题?