Skip to content

Commit

Permalink
Added support for ASGI_THREADS max worker limit. (#422)
Browse files Browse the repository at this point in the history
Closes #319
  • Loading branch information
carltongibson authored Jul 6, 2022
1 parent 6199d50 commit 71ba440
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ Unreleased

* Added ``--log-fmt`` CLI argument.

* Added support for ``ASGI_THREADS`` environment variable, setting the maximum
number of workers used by a ``SyncToAsync`` thread-pool executor.

Set e.g. ``ASGI_THREADS=4 daphne ...`` when running to limit the number of
workers.

3.0.2 (2021-04-07)
------------------

Expand Down
8 changes: 8 additions & 0 deletions daphne/server.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
# This has to be done first as Twisted is import-order-sensitive with reactors
import asyncio # isort:skip
import os # isort:skip
import sys # isort:skip
import warnings # isort:skip
from concurrent.futures import ThreadPoolExecutor # isort:skip
from twisted.internet import asyncioreactor # isort:skip


twisted_loop = asyncio.new_event_loop()
if "ASGI_THREADS" in os.environ:
twisted_loop.set_default_executor(
ThreadPoolExecutor(max_workers=int(os.environ["ASGI_THREADS"]))
)

current_reactor = sys.modules.get("twisted.internet.reactor", None)
if current_reactor is not None:
if not isinstance(current_reactor, asyncioreactor.AsyncioSelectorReactor):
Expand Down
12 changes: 11 additions & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
import os
from argparse import ArgumentError
from unittest import TestCase
from unittest import TestCase, skipUnless

from daphne.cli import CommandLineInterface
from daphne.endpoints import build_endpoint_description_strings as build
Expand Down Expand Up @@ -255,3 +256,12 @@ def test_no_servername(self):
Passing `--no-server-name` will set server name to '' (empty string)
"""
self.assertCLI(["--no-server-name"], {"server_name": ""})


@skipUnless(os.getenv("ASGI_THREADS"), "ASGI_THREADS environment variable not set.")
class TestASGIThreads(TestCase):
def test_default_executor(self):
from daphne.server import twisted_loop

executor = twisted_loop._default_executor
self.assertEqual(executor._max_workers, int(os.getenv("ASGI_THREADS")))

0 comments on commit 71ba440

Please sign in to comment.