我在Django中有一个简单的测试用例失败:
模型(app / models.py):
from django.db import models
class M(models.Model):
condition = models.CharField(max_length=80, db_index=True)
测试(app / tests.py):
from django.test import TestCase
import threading
import time
from app.models import M
def insert():
time.sleep(0.3)
ua = M(condition='x')
ua.save()
class DjangoRaceTest(TestCase):
def test_parallel(self):
insert() # <--- works
#threading.Thread(target=insert).start() # <-- fails
for i in range(10):
count = M.objects.all()
if count:
return True
time.sleep(0.1)
assert count
上面的代码通过;但是如果我注释掉插入的直接调用而不是取消注释线程调用,则断言失败,我得到:
Creating test database for alias 'default'...
Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib/python2.6/threading.py", line 532, in __bootstrap_inner
self.run()
File "/usr/lib/python2.6/threading.py", line 484, in run
self.__target(*self.__args, **self.__kwargs)
File "/home/jacobsen/testdjango/app/tests.py", line 10, in insert
ua.save()
File "/home/jacobsen/dj14/lib/python2.6/site-packages/Django-1.4-py2.6.egg/django/db/models/base.py", line 463, in save
self.save_base(using=using, force_insert=force_insert, force_update=force_update)
File "/home/jacobsen/dj14/lib/python2.6/site-packages/Django-1.4-py2.6.egg/django/db/models/base.py", line 551, in save_base
result = manager._insert([self], fields=fields, return_id=update_pk, using=using, raw=raw)
File "/home/jacobsen/dj14/lib/python2.6/site-packages/Django-1.4-py2.6.egg/django/db/models/manager.py", line 203, in _insert
return insert_query(self.model, objs, fields, **kwargs)
File "/home/jacobsen/dj14/lib/python2.6/site-packages/Django-1.4-py2.6.egg/django/db/models/query.py", line 1576, in insert_query
return query.get_compiler(using=using).execute_sql(return_id)
File "/home/jacobsen/dj14/lib/python2.6/site-packages/Django-1.4-py2.6.egg/django/db/models/sql/compiler.py", line 910, in execute_sql
cursor.execute(sql, params)
File "/home/jacobsen/dj14/lib/python2.6/site-packages/Django-1.4-py2.6.egg/django/db/backends/sqlite3/base.py", line 337, in execute
return Database.Cursor.execute(self, query, params)
DatabaseError: no such table: app_m
显然有一些Django魔法在这里让我失望,或者......某事?我已经在OS X,Ubuntu 10.04,Django 1.2和1.4的各种组合上进行了测试。
答案 0 :(得分:2)
答案似乎是在内存中,sqlite数据库不能跨线程共享。
This is a bug report from 1.1核心开发人员认为它不是基于SQLAlchemy's lack of support的错误(在没有来自sqlite引用的其他直接信息的情况下):
Pysqlite连接不支持在线程之间移动, 除非check_same_thread Pysqlite标志设置为False。在 另外,在使用内存中的SQLite数据库时,还要使用完整的数据库 仅存在于单个连接的范围内。据报道 内存数据库不支持在它们之间共享 线程无论check_same_thread标志 - 这意味着a 多线程应用程序无法共享来自:memory:database的数据 跨线程,除非访问连接仅限于一个 工作线程通过排队机制进行通信 并发线程。