On 24 oct, 21:52, BlueBird <p...@freehackers.org> wrote:
> Hi,
>
> I've got a strange behavior. My application is split between a web
> frontend and a backend which use the DB to exchange tasks and results.
>
> The problem is with the backend. It's just a regular program,
> accessing the DB to fetch tasks, completing them and then storing the
> result in a DB. The program may (depending on a config) use multiple
> threads to perform the tasks and store the result.
>
> When running the test suite that exercise the backend, I get an error
> if I allow multiple threads of execution :
>
I've reduced the problem to one script, using the Poll application
from the tutorial.
The problem seems to be that when running in test mode with threads,
the behavior is
different than when running in test mode without threads, and the DB
is not set correctly
for the background thread.
The demonstration with a simple script, to run in the Poll tutorial:
==================================
import os
os.environ[ 'DJANGO_SETTINGS_MODULE' ] = 'settings'
import settings
import datetime, threading
#django stuff
from polls.models import *
from django.core.mail import mail_admins
from django.test.utils import *
from django.db import connection
def create_object():
print 'Creating Poll'
p = Poll()
p.question = "What's up doc ?"
p.pub_date = datetime.date.today()
p.save()
print 'Poll object saved. Id: %d' % p.id
WITH_THREAD = True
if __name__ == '__main__':
setup_test_environment()
old_db_name = settings.DATABASE_NAME
new_db_name = connection.creation.create_test_db(verbosity=1)
print 'New DATABASE:', new_db_name
if WITH_THREAD:
t = threading.Thread( target=create_object )
t.start()
t.join()
else:
create_object()
teardown_test_environment()
connection.creation.destroy_test_db( old_db_name )
=====================================
With WITH_THREAD set to True:
=====================================
phili...@pc-philippe /cygdrive/d/work/django/django-tuto/mysite $
python
run_with_threads.py
Creating test
database...
Creating table
auth_permission
Creating table
auth_group
Creating table
auth_user
Creating table
auth_message
Creating table
django_admin_log
Creating table
django_content_type
Creating table
django_session
Creating table
django_site
Creating table
polls_poll
Creating table
polls_choice
Installing index for auth.Permission
model
Installing index for auth.Message
model
Installing index for admin.LogEntry
model
Installing index for polls.Choice
model
New
DATABASE: :memory:
Creating
Poll
Exception in thread
Thread-1:
Traceback (most recent call
last):
File "c:\Python26\lib\threading.py", line 522, in
__bootstrap_inner
self.run
()
File "c:\Python26\lib\threading.py", line 477, in
run
[...]
File "c:\Python26\lib\site-packages\django\db\backends
\sqlite3\base.py", line
193, in
execute
return Database.Cursor.execute(self, query,
params)
OperationalError: no such table:
polls_poll
Destroying test
database...
phili...@pc-philippe /cygdrive/d/work/django/django-tuto/mysite
$
==============================
With WITH_THREAD set to False:
==============================
phili...@pc-philippe /cygdrive/d/work/django/django-tuto/mysite $
python run_wi
th_threads.py
Creating test
database...
Creating table
auth_permission
Creating table
auth_group
Creating table
auth_user
Creating table
auth_message
Creating table
django_admin_log
Creating table
django_content_type
Creating table
django_session
Creating table
django_site
Creating table
polls_poll
Creating table
polls_choice
Installing index for auth.Permission
model
Installing index for auth.Message
model
Installing index for admin.LogEntry
model
Installing index for polls.Choice
model
New
DATABASE: :memory:
Creating
Poll
Poll object saved. Id:
1
Destroying test
database...
phili...@pc-philippe /cygdrive/d/work/django/django-tuto/mysite
$
===============================
I've tried to increase the verbosity of create_test_db() and compare
the output in the two
cases. No difference !
Any idea ? Looks like a django bug to me in the in-memory DB used for
tests.
Philippe
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---