|
4 | 4 | NOTE: this module is only imported if we need to format several files at once.
|
5 | 5 | """
|
6 | 6 |
|
| 7 | +from __future__ import annotations |
| 8 | + |
7 | 9 | import asyncio
|
8 | 10 | import logging
|
9 | 11 | import os
|
@@ -80,20 +82,25 @@ def reformat_many(
|
80 | 82 | """Reformat multiple files using a ProcessPoolExecutor."""
|
81 | 83 | maybe_install_uvloop()
|
82 | 84 |
|
83 |
| - executor: Executor |
84 | 85 | if workers is None:
|
85 | 86 | workers = int(os.environ.get("BLACK_NUM_WORKERS", 0))
|
86 | 87 | workers = workers or os.cpu_count() or 1
|
87 | 88 | if sys.platform == "win32":
|
88 | 89 | # Work around https://bugs.python.org/issue26903
|
89 | 90 | workers = min(workers, 60)
|
90 |
| - try: |
91 |
| - executor = ProcessPoolExecutor(max_workers=workers) |
92 |
| - except (ImportError, NotImplementedError, OSError): |
93 |
| - # we arrive here if the underlying system does not support multi-processing |
94 |
| - # like in AWS Lambda or Termux, in which case we gracefully fallback to |
95 |
| - # a ThreadPoolExecutor with just a single worker (more workers would not do us |
96 |
| - # any good due to the Global Interpreter Lock) |
| 91 | + |
| 92 | + executor: Executor | None = None |
| 93 | + if workers > 1: |
| 94 | + try: |
| 95 | + executor = ProcessPoolExecutor(max_workers=workers) |
| 96 | + except (ImportError, NotImplementedError, OSError): |
| 97 | + # we arrive here if the underlying system does not support multi-processing |
| 98 | + # like in AWS Lambda or Termux, in which case we gracefully fallback to |
| 99 | + # a ThreadPoolExecutor with just a single worker (more workers would not do |
| 100 | + # us any good due to the Global Interpreter Lock) |
| 101 | + pass |
| 102 | + |
| 103 | + if executor is None: |
97 | 104 | executor = ThreadPoolExecutor(max_workers=1)
|
98 | 105 |
|
99 | 106 | loop = asyncio.new_event_loop()
|
|
0 commit comments