これが私のウェブサイトからの関連コードです__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がアプリケーションをスレッド化していることに気づいていませんでした:/
どうすればこれを修正できますか?