mirror of
https://github.com/django/daphne.git
synced 2025-04-21 01:02:06 +03:00
Fix runserver to properly autoreload w/Twisted
This commit is contained in:
parent
1a7010aa2c
commit
a4381f427e
|
@ -17,7 +17,6 @@ def setup_logger(name, verbosity=1):
|
|||
logger.addHandler(handler)
|
||||
if verbosity > 1:
|
||||
logger.setLevel(logging.DEBUG)
|
||||
logger.debug("Logging set to DEBUG")
|
||||
|
||||
# Set up daphne protocol loggers
|
||||
for module in ["daphne.ws_protocol", "daphne.http_protocol"]:
|
||||
|
|
|
@ -18,24 +18,25 @@ class Command(RunserverCommand):
|
|||
self.logger = setup_logger('django.channels', self.verbosity)
|
||||
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
|
||||
self.channel_layer = channel_layers[DEFAULT_CHANNEL_LAYER]
|
||||
if not self.channel_layer.registry.consumer_for_channel("http.request"):
|
||||
self.channel_layer.registry.add_consumer(ViewConsumer(), ["http.request"])
|
||||
# Helpful note to say this is the Channels runserver
|
||||
self.logger.info("Worker thread running, channels enabled")
|
||||
# Report starting up
|
||||
# Launch worker as subthread (including autoreload logic)
|
||||
worker = WorkerThread(self.channel_layer)
|
||||
worker = WorkerThread(self.channel_layer, self.logger)
|
||||
worker.daemon = True
|
||||
worker.start()
|
||||
# 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)
|
||||
self.logger.info("Daphne running, listening on %s:%s", self.addr, self.port)
|
||||
from daphne.server import Server
|
||||
Server(
|
||||
channel_layer=self.channel_layer,
|
||||
host=self.addr,
|
||||
port=int(self.port),
|
||||
signal_handlers=False,
|
||||
).run()
|
||||
|
||||
|
||||
|
@ -44,13 +45,12 @@ class WorkerThread(threading.Thread):
|
|||
Class that runs a worker
|
||||
"""
|
||||
|
||||
def __init__(self, channel_layer):
|
||||
def __init__(self, channel_layer, logger):
|
||||
super(WorkerThread, self).__init__()
|
||||
self.channel_layer = channel_layer
|
||||
self.logger = logger
|
||||
|
||||
def run(self):
|
||||
self.logger.info("Worker thread running")
|
||||
worker = Worker(channel_layer=self.channel_layer)
|
||||
# We need to set run_main so it doesn't try to relaunch the entire
|
||||
# program - that will make Daphne run twice.
|
||||
os.environ["RUN_MAIN"] = "true"
|
||||
autoreload.main(worker.run)
|
||||
worker.run()
|
||||
|
|
Loading…
Reference in New Issue
Block a user