88"""
99
1010import difflib
11+ import functools
1112import os .path
1213import re
1314import subprocess
1415import sys
1516import time
1617import traceback
17- from concurrent .futures import as_completed , Executor , ProcessPoolExecutor
18+ from concurrent .futures import as_completed , Executor
1819from copy import deepcopy
1920from dataclasses import dataclass , replace
2021from multiprocessing import cpu_count
2122from pathlib import Path
22- from typing import Any , AnyStr , cast , Dict , List , Optional , Sequence , Union
23+ from typing import Any , AnyStr , Callable , cast , Dict , List , Optional , Sequence , Union
2324
2425from libcst import parse_module , PartialParserConfig
2526from libcst .codemod ._codemod import Codemod
@@ -608,14 +609,20 @@ def parallel_exec_transform_with_prettyprint( # noqa: C901
608609 python_version = python_version ,
609610 )
610611
611- pool_impl : type [ Executor ]
612+ pool_impl : Callable [[], Executor ]
612613 if total == 1 or jobs == 1 :
613614 # Simple case, we should not pay for process overhead.
614615 # Let's just use a dummy synchronous executor.
615616 jobs = 1
616617 pool_impl = DummyExecutor
618+ elif getattr (sys , "_is_gil_enabled" , lambda : False )(): # pyre-ignore[16]
619+ from concurrent .futures import ThreadPoolExecutor
620+
621+ pool_impl = functools .partial (ThreadPoolExecutor , max_workers = jobs )
617622 else :
618- pool_impl = ProcessPoolExecutor
623+ from concurrent .futures import ProcessPoolExecutor
624+
625+ pool_impl = functools .partial (ProcessPoolExecutor , max_workers = jobs )
619626 # Warm the parser, pre-fork.
620627 parse_module (
621628 "" ,
@@ -631,7 +638,7 @@ def parallel_exec_transform_with_prettyprint( # noqa: C901
631638 warnings : int = 0
632639 skips : int = 0
633640
634- with pool_impl (max_workers = jobs ) as executor : # type: ignore
641+ with pool_impl () as executor : # type: ignore
635642 args = [
636643 {
637644 "transformer" : transform ,
0 commit comments