我正在尝试将一个应用程序从django 1.11.1迁移到django 2.0.1测试设置为在内存数据库中使用sqlite运行。但是每个测试都失败了,因为每个表都有sqlite3.OperationalError: database table is locked。我怎么才能知道为什么它被锁住了?增加超时设置无济于事。
我使用的是LiveServerTestCase,所以我认为测试一定是在与内存数据库不同的线程中运行的,并且由于某种原因没有被共享。
发布于 2018-08-08 23:32:31
我也碰到这个了。从this got merged开始,LiveServerTestCase就是多线程的。
当我的被测应用程序发出多个请求时,这对我来说就成了一个问题。然后,所以我猜测,LiveServer会派生线程来处理这些请求。然后,这些请求导致对SQLite数据库的写入。这反过来又不喜欢多个写入线程。
有趣的是,runserver知道--nothreading。但是测试服务器似乎缺少这样的选项。
下面的代码片段为我带来了一个单线程测试服务器:
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派生测试类。
发布于 2018-01-27 02:23:31
它是由this django bug引起的。
发布于 2018-07-30 22:58:10
在测试期间使用基于文件的数据库可以修复“表已锁定”错误。要使Django使用基于文件的数据库,请将其文件名指定为test database name:
DATABASES = {
'default': {
...
'TEST': {
'NAME': os.path.join(BASE_DIR, 'db.sqlite3.test'),
},
}
}我认为在内存中数据库的情况下,超时设置被忽略了,请参见此comment for additional info。
https://stackoverflow.com/questions/48353002
复制相似问题