Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: parametrize the number of threads used for compression #1192

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 15 additions & 4 deletions compressor/management/commands/compress.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@ def add_arguments(self, parser):
"multiple engines. If not specified, django engine is used.",
dest="engines",
)
parser.add_argument(
"-t",
"--threads",
default=4,
type=int,
help="Specifies the number of threads in the thread pool used for"
"generation",
dest="threads"
)

def get_loaders(self):
template_source_loaders = []
Expand Down Expand Up @@ -123,7 +132,7 @@ def __get_parser(self, engine):

return parser

def compress(self, engine, extensions, verbosity, follow_links, log):
def compress(self, engine, extensions, verbosity, follow_links, log, threads):
"""
Searches templates containing 'compress' nodes and compresses them
"offline" -- outside of the request/response cycle.
Expand Down Expand Up @@ -271,8 +280,9 @@ def compress(self, engine, extensions, verbosity, follow_links, log):
for node in nodes:
nodes_count += 1
template_nodes.setdefault(node, []).append(context)

pool = concurrent.futures.ThreadPoolExecutor(max_workers=4)
if verbosity >= 2:
log.write("Compressing with %d threads\n" % threads)
pool = concurrent.futures.ThreadPoolExecutor(max_workers=threads)
for template, nodes in compressor_nodes.items():
template._log = log
template._log_verbosity = verbosity
Expand Down Expand Up @@ -387,13 +397,14 @@ def handle_inner(self, **options):
follow_links = options.get("follow_links", False)
extensions = self.handle_extensions(options.get("extensions") or ["html"])
engines = [e.strip() for e in options.get("engines", [])] or ["django"]
threads = options.get('threads')

final_offline_manifest = {}
final_block_count = 0
final_results = []
for engine in engines:
offline_manifest, block_count, results = self.compress(
engine, extensions, verbosity, follow_links, log
engine, extensions, verbosity, follow_links, log, threads
)
final_results.extend(results)
final_block_count += block_count
Expand Down