问题
Navigating to the page test?x=a
defined below it works.
Navigating to test?x=a
and then quickly navigating to test?x=b
, both the cycles will keep running for a few seconds, but one of them will eventually crash with the error peewee.OperationalError: cannot start a transaction within a transaction
.
This is obviously not a real world test, it is a way to reproduce the real world problems that I am having once in a while.
In the real world application I see errors similar to this one when background tasks run or when the user types on a text box and many requests are quickly fired with ajax.
db = peewee.SqliteDatabase('db', check_same_thread=False)
class Test(peewee.Model):
field = peewee.CharField()
class Meta:
database = db
Test.drop_table(True)
Test.create_table(True)
class MyApp:
@cherrypy.expose
def test(self, x):
for i in range(1000):
Test.create(field='%s %03d' % (x, i))
time.sleep(0.1)
I already asked this question, but I did not have an example that would reproduce the problem.
回答1:
Instead of check_same_thread=False
try using threadlocals=True
.
来源:https://stackoverflow.com/questions/25850681/cherrypy-sqlite3-peewee-crashes-when-two-processes-execute-the-same-code-at