当两个进程同时执行相同的代码时,Cherrypy + sqlite3 + Peewee崩溃 [英] Cherrypy + sqlite3 + Peewee crashes when two processes execute the same code at the same time

查看:60
本文介绍了当两个进程同时执行相同的代码时,Cherrypy + sqlite3 + Peewee崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

导航到下面定义的页面 test?x = a 即可.

Navigating to the page test?x=a defined below it works.

导航到 test?x = a ,然后快速导航至 test?x = b ,这两个周期将持续运行几秒钟,但是其中一个周期最终将崩溃,并显示错误 peewee.OperationalError:无法在事务内启动事务.

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.

在真实的应用程序中,当运行后台任务或用户在文本框中键入内容并且使用ajax快速触发许多请求时,我会看到与该错误类似的错误.

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.

推荐答案

而不是 check_same_thread = False ,请尝试使用 threadlocals = True .

这篇关于当两个进程同时执行相同的代码时,Cherrypy + sqlite3 + Peewee崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆