Skip to content

Instantly share code, notes, and snippets.

@kane-c
Created March 1, 2015 00:56
Show Gist options
  • Save kane-c/191cde363fe555cd4fb0 to your computer and use it in GitHub Desktop.
Save kane-c/191cde363fe555cd4fb0 to your computer and use it in GitHub Desktop.
# Make the Django server from LiveServerTestCase multi-threaded
# so that you can make requests to yourself during tests without
# deadlocking.
# Note that this prevents being able to use in-memory sqlite for
# tests unless you use something like `/dev/shm`
# May cause other bugs too
from django.core.servers.basehttp import WSGIServer
from django.test import testcases
from django.utils.six.moves import socketserver
testcases.WSGIServer = type(str('WSGIServer'),
(socketserver.ThreadingMixIn, WSGIServer), {})
@sandymantri
Copy link

sandymantri commented Jul 27, 2017

Hi Kane,
I am trying to use this snippet to implement threaded server for my django test runner. We have custom sql_flush function which we want to call after each test-case execution. It works fine when I am not using above snippet(no multi threaded approach), but when i put above snippet to my settings file, the default sql_flush is called from django library. Below is the code snippet. Can you help me fixing this?

from django.core.servers.basehttp import WSGIServer
from django.test import testcases
from django.utils.six.moves import socketserver
testcases.WSGIServer = type(str('WSGIServer'), (socketserver.ThreadingMixIn, WSGIServer), {})

from django.core.management import sql
sql.sql_flush = sql_flush_without_cms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment