Skip to content

Commit b447349

Browse files
committed
[fix] Create a shim class providing compat for Pool implementations
On Windows and MacOS the multiprocess library's Pool class is used, whereas on Linux and other platforms the concurrent.futures library's ProcessPoolExecutor is used. These two classes have similar but not identical APIs. This commit introduces a shim class which adapts the API of multiprocess.Pool to be more similar to ProcessPoolExecutor. This commit will close Ericsson#4338.
1 parent a9e7564 commit b447349

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

codechecker_common/compatibility/multiprocessing.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,17 @@
1313
# pylint: disable=no-name-in-module
1414
# pylint: disable=unused-import
1515
if sys.platform in ["darwin", "win32"]:
16-
from multiprocess import Pool # type: ignore
16+
from multiprocess.pool import Pool as MultiprocessPool
1717
from multiprocess import cpu_count
18+
19+
# Shim class intended to provide API compatibility between the
20+
# pools from multiprocess and concurrent.futures.
21+
class Pool(MultiprocessPool): # type: ignore
22+
def __init__(self, *args, max_workers=None, **kwargs):
23+
super().__init__(*args, processes=max_workers, **kwargs)
24+
25+
def map(self, fn, *iterables, chunksize=1):
26+
return super().starmap(fn, zip(*iterables), chunksize)
1827
else:
1928
from concurrent.futures import ProcessPoolExecutor as Pool # type: ignore
2029
from multiprocessing import cpu_count

0 commit comments

Comments
 (0)