Skip to content

Instantly share code, notes, and snippets.

@piotrkilczuk
Created August 9, 2011 12:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save piotrkilczuk/1133889 to your computer and use it in GitHub Desktop.
Save piotrkilczuk/1133889 to your computer and use it in GitHub Desktop.
Run multiple instances of Django development server using subprocesses
#!/usr/bin/env python
import glob
import os.path
import socket
import subprocess
import sys
# manage.py must be made executable in order for this to work
# an interable of tuples in format:
# (SERVER_IP, SERVER_PORT, DJANGO_SETTINGS)
# SERVER_IP and/or SERVER_PORT can be None
SERVERS = (
('127.0.0.4', None, 'root_settings'),
('127.0.0.4', None, 'redakcja_settings'),
)
# at which port to start attempting binding, when SERVER_PORT not in tuple
PORT_START = 8000
# you might want to run runserver_plus when using django-extensions
COMMAND = 'runserver'
################################################################################
# it shouldn't be necessary to edit below this line ############################
################################################################################
cwd = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
servers_clean = []
last_port = PORT_START
for server in SERVERS:
ip = server[0] or '127.0.0.1'
if server[1]:
port = server[1]
else:
while True:
try:
s = socket.socket()
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((ip, last_port))
s.close()
break
except socket.error:
last_port += 1
port = last_port
last_port += 1
settings = server[2]
manage_py = glob.glob(cwd + '/*/manage.py')
try:
subprocess.Popen((manage_py[0],
COMMAND,
'%s:%s' % (ip, port),
'--settings=%s' % settings), cwd=cwd)
except IndexError:
print 'Cannot find manage.py'
sys.exit(1)
try:
while True:
pass
except KeyboardInterrupt:
print 'All %d servers terminated' % len(SERVERS)
@jacopsd
Copy link

jacopsd commented Nov 21, 2023

This file has incorrect indentation

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