Cherrypy + sqlite3 + Peewee crashes when two proce

2019-08-21 19:39发布

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条回答
成全新的幸福
2楼-- · 2019-08-21 20:33

Instead of check_same_thread=False try using threadlocals=True.

查看更多
登录 后发表回答