Fix runserver to properly autoreload w/Twisted

This commit is contained in:
Andrew Godwin 2016-02-10 19:24:18 +00:00
parent 1a7010aa2c
commit a4381f427e
2 changed files with 9 additions and 10 deletions

View File

@ -17,7 +17,6 @@ def setup_logger(name, verbosity=1):
logger.addHandler(handler) logger.addHandler(handler)
if verbosity > 1: if verbosity > 1:
logger.setLevel(logging.DEBUG) logger.setLevel(logging.DEBUG)
logger.debug("Logging set to DEBUG")
# Set up daphne protocol loggers # Set up daphne protocol loggers
for module in ["daphne.ws_protocol", "daphne.http_protocol"]: for module in ["daphne.ws_protocol", "daphne.http_protocol"]:

View File

@ -18,24 +18,25 @@ class Command(RunserverCommand):
self.logger = setup_logger('django.channels', self.verbosity) self.logger = setup_logger('django.channels', self.verbosity)
super(Command, self).handle(*args, **options) super(Command, self).handle(*args, **options)
def run(self, *args, **options): def inner_run(self, *args, **options):
# Check a handler is registered for http reqs; if not, add default one # Check a handler is registered for http reqs; if not, add default one
self.channel_layer = channel_layers[DEFAULT_CHANNEL_LAYER] self.channel_layer = channel_layers[DEFAULT_CHANNEL_LAYER]
if not self.channel_layer.registry.consumer_for_channel("http.request"): if not self.channel_layer.registry.consumer_for_channel("http.request"):
self.channel_layer.registry.add_consumer(ViewConsumer(), ["http.request"]) self.channel_layer.registry.add_consumer(ViewConsumer(), ["http.request"])
# Helpful note to say this is the Channels runserver # Report starting up
self.logger.info("Worker thread running, channels enabled")
# Launch worker as subthread (including autoreload logic) # Launch worker as subthread (including autoreload logic)
worker = WorkerThread(self.channel_layer) worker = WorkerThread(self.channel_layer, self.logger)
worker.daemon = True worker.daemon = True
worker.start() worker.start()
# Launch server in main thread (Twisted doesn't like being in a # Launch server in main thread (Twisted doesn't like being in a
# subthread, and it doesn't need to autoreload as there's no user code) # subthread, and it doesn't need to autoreload as there's no user code)
self.logger.info("Daphne running, listening on %s:%s", self.addr, self.port)
from daphne.server import Server from daphne.server import Server
Server( Server(
channel_layer=self.channel_layer, channel_layer=self.channel_layer,
host=self.addr, host=self.addr,
port=int(self.port), port=int(self.port),
signal_handlers=False,
).run() ).run()
@ -44,13 +45,12 @@ class WorkerThread(threading.Thread):
Class that runs a worker Class that runs a worker
""" """
def __init__(self, channel_layer): def __init__(self, channel_layer, logger):
super(WorkerThread, self).__init__() super(WorkerThread, self).__init__()
self.channel_layer = channel_layer self.channel_layer = channel_layer
self.logger = logger
def run(self): def run(self):
self.logger.info("Worker thread running")
worker = Worker(channel_layer=self.channel_layer) worker = Worker(channel_layer=self.channel_layer)
# We need to set run_main so it doesn't try to relaunch the entire worker.run()
# program - that will make Daphne run twice.
os.environ["RUN_MAIN"] = "true"
autoreload.main(worker.run)