diff options
author | Omer Katz <omer.drow@gmail.com> | 2017-12-09 13:21:20 +0200 |
---|---|---|
committer | Omer Katz <omer.drow@gmail.com> | 2017-12-09 13:21:20 +0200 |
commit | 3acca30e28152d82b2a68b88f947743051bdc6af (patch) | |
tree | 81f837612716c6a0fd5a210e41ce72db0021b9d3 | |
parent | a88e2a6e5d37f47063a9fb0f32db45a96b22979c (diff) | |
download | isort-3acca30e28152d82b2a68b88f947743051bdc6af.tar.gz |
Process files in parallel.
-rwxr-xr-x | isort/main.py | 42 |
1 files changed, 37 insertions, 5 deletions
diff --git a/isort/main.py b/isort/main.py index eae7afa5..118ab693 100755 --- a/isort/main.py +++ b/isort/main.py @@ -24,6 +24,8 @@ import argparse import glob import os import sys +from concurrent.futures import ProcessPoolExecutor +import functools import setuptools @@ -57,6 +59,21 @@ INTRO = r""" """.format(__version__) +class SortAttempt(object): + def __init__(self, incorrectly_sorted, skipped): + self.incorrectly_sorted = incorrectly_sorted + self.skipped = skipped + + +def sort_imports(file_name, **arguments): + try: + result = SortImports(file_name, **arguments) + return SortAttempt(result.incorrectly_sorted, result.skipped) + except IOError as e: + print("WARNING: Unable to parse file {0} due to {1}".format(file_name, e)) + return None + + def iter_source_code(paths, config, skipped): """Iterate over all Python source files defined in paths.""" for path in paths: @@ -235,6 +252,8 @@ def create_parser(): parser.add_argument('-lbt', '--lines-between-types', dest='lines_between_types', type=int) parser.add_argument('-up', '--use-parentheses', dest='use_parentheses', action='store_true', help='Use parenthesis for line continuation on lenght limit instead of slashes.') + parser.add_argument('-j', '--jobs', help='Number of files to process in parallel.', + dest='jobs', type=int) arguments = dict((key, value) for (key, value) in itemsview(vars(parser.parse_args())) if value) if 'dont_order_by_type' in arguments: @@ -270,16 +289,29 @@ def main(): num_skipped = 0 if config['verbose'] or config.get('show_logo', False): print(INTRO) - for file_name in file_names: - try: - sort_attempt = SortImports(file_name, **arguments) + jobs = arguments.get('jobs') + if jobs: + executor = ProcessPoolExecutor(max_workers=jobs) + + for sort_attempt in executor.map(functools.partial(sort_imports, **arguments), file_names): + if not sort_attempt: + continue incorrectly_sorted = sort_attempt.incorrectly_sorted if arguments.get('check', False) and incorrectly_sorted: wrong_sorted_files = True if sort_attempt.skipped: num_skipped += 1 - except IOError as e: - print("WARNING: Unable to parse file {0} due to {1}".format(file_name, e)) + else: + for file_name in file_names: + try: + sort_attempt = SortImports(file_name, **arguments) + incorrectly_sorted = sort_attempt.incorrectly_sorted + if arguments.get('check', False) and incorrectly_sorted: + wrong_sorted_files = True + if sort_attempt.skipped: + num_skipped += 1 + except IOError as e: + print("WARNING: Unable to parse file {0} due to {1}".format(file_name, e)) if wrong_sorted_files: exit(1) |