From 079558d5b5d12f28135a67e2e53bd669579fe249 Mon Sep 17 00:00:00 2001 From: Luke Hodkinson Date: Sat, 3 Sep 2016 01:02:29 +1000 Subject: [PATCH] Too many threads bug in threaded worker. (#334) * Use a mixin for common test-case code. This way we can have both a regular channels test-case, and a transaction test-case, too. * Adding a reference to django-cq. * Adding the ability to launch a number of workers in threads. This is to try and help reduce memory consumption. * Adding a signal for process level worker startups. * Cleaning up the threaded worker code. * Use Python 2.7 friendly code. * Making the runworker command show a little more information about how many threads are running. * Moving the worker ready signal into a method in order to support polymorphic behavior. * Ugh, I'm an idiot. Was launching the wrong run. * Adding a call to the workers' `ready` in `runserver`. * Fixed a bug whereby too many threads were being used when threaded workers were used. --- channels/management/commands/runworker.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/channels/management/commands/runworker.py b/channels/management/commands/runworker.py index 84454a1..9823b4c 100644 --- a/channels/management/commands/runworker.py +++ b/channels/management/commands/runworker.py @@ -59,12 +59,14 @@ class Command(BaseCommand): self.callback = callback self.options = options # Choose an appropriate worker. + worker_kwargs = {} if self.n_threads == 1: self.logger.info("Using single-threaded worker.") worker_cls = Worker else: self.logger.info("Using multi-threaded worker, {} thread(s).".format(self.n_threads)) worker_cls = WorkerGroup + worker_kwargs['n_threads'] = self.n_threads # Run the worker self.logger.info("Running worker against channel layer %s", self.channel_layer) try: @@ -73,6 +75,7 @@ class Command(BaseCommand): callback=self.callback, only_channels=self.options.get("only_channels", None), exclude_channels=self.options.get("exclude_channels", None), + **worker_kwargs ) worker_process_ready.send(sender=worker) worker.ready()