sqlite数据库表在测试中被锁定 [英] sqlite database table is locked on tests

查看:354
本文介绍了sqlite数据库表在测试中被锁定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将应用程序从django 1.11.1迁移到django 2.0.1
测试已设置为在内存数据库中使用sqlite运行。但是每个测试都失败了,因为每个表 sqlite3.OperationalError:数据库表已锁定。我如何找出为什么被锁定?增加超时设置无济于事。

I am trying to migrate an application from django 1.11.1 to django 2.0.1 Tests are set up to run with sqlite in memory database. But every test is failing, because sqlite3.OperationalError: database table is locked for every table. How can I find out why is it locked? Icreasing timeout setting does not help.

我正在使用 LiveServerTestCase ,因此我认为测试必须在与内存数据库不同的线程,并且由于某种原因它不会共享。

I am using LiveServerTestCase, so I suppose the tests must be running in a different thread than the in memory database, and it for some reason does not get shared.

推荐答案

我也遇到了这个问题。 LiveServerTestCase 是多线程的,因为合并

I hit this, too. The LiveServerTestCase is multi-threaded since this got merged.

当我的测试中的应用发出多个请求时,这对我来说是个问题。然后,据我的推测,LiveServer生成线程来处理这些请求。然后,这些请求将导致写入SQLite数据库。这样反过来又不喜欢多个写入线程。

It becomes a problem for me when my app under test issues multiple requests. Then, so my speculation, the LiveServer spawns threads to handle those requests. Those requests then cause a write to the SQLite db. That in turn does not like multiple writing threads.

很有趣的是, runserver 知道 --nothreading 。但是测试服务器似乎缺少这种选择。

Funnily enough, runserver knows about --nothreading. But such an option seems to be missing for the test server.

以下代码段为我带来了单线程测试服务器:

The following snippet brought me a single-threaded test server:

class LiveServerSingleThread(LiveServerThread):
    """Runs a single threaded server rather than multi threaded. Reverts https://github.com/django/django/pull/7832"""

    def _create_server(self):

        """
        the keep-alive fixes introduced in Django 2.1.4 (934acf1126995f6e6ccba5947ec8f7561633c27f)
        cause problems when serving the static files in a stream.
        We disable the helper handle method that calls handle_one_request multiple times.
        """
        QuietWSGIRequestHandler.handle = QuietWSGIRequestHandler.handle_one_request

        return WSGIServer((self.host, self.port), QuietWSGIRequestHandler, allow_reuse_address=False)


class LiveServerSingleThreadedTestCase(LiveServerTestCase):
    "A thin sub-class which only sets the single-threaded server as a class"
    server_thread_class = LiveServerSingleThread

然后,从 LiveServerSingleThreadedTestCase 而不是 LiveServerTestCase 派生测试类。

Then, derive your test class from LiveServerSingleThreadedTestCase instead of LiveServerTestCase.

这篇关于sqlite数据库表在测试中被锁定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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