0

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.

Community
  • 1
  • 1
stenci
  • 8,290
  • 14
  • 64
  • 104

1 Answers1

1

Instead of check_same_thread=False try using threadlocals=True.

coleifer
  • 24,887
  • 6
  • 60
  • 75
  • Thanks. The documentation recommends it: http://peewee.readthedocs.org/en/2.0.2/peewee/cookbook.html#multi-threaded-applications – stenci Sep 15 '14 at 15:41