Cherrypy + sqlite3 + Peewee crashes when two processes execute the same code at the same time

为君一笑 提交于 2019-12-11 18:57:56

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!