From 53718d9cb09a2949e907e71d07c8caa4500c1312 Mon Sep 17 00:00:00 2001 From: Justin Date: Fri, 29 Nov 2019 07:14:16 -0800 Subject: [PATCH] fallback to threads if no semaphore support (#277) Platforms like Android Termux do not have support for semaphores and thus cannot use `multiprocessing.Pool` Use the multiprocessing.dummy package as a fallback, which provides the Pool API backed by a ThreadPool. Fixes #229 --- src/pipx/commands/commands.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/pipx/commands/commands.py b/src/pipx/commands/commands.py index 61d32062ff..e36ef6b099 100644 --- a/src/pipx/commands/commands.py +++ b/src/pipx/commands/commands.py @@ -3,7 +3,6 @@ import datetime import hashlib import logging -import multiprocessing import shlex import shutil import subprocess @@ -16,6 +15,19 @@ from shutil import which from typing import List +try: + # Instantiating a Pool() attempts to import multiprocessing.synchronize, + # which fails if the underlying OS does not support semaphores. + # Here, we import ahead of time to decide which Pool implementation to use: + # one backed by Processes (the default), or one backed by Threads + import multiprocessing.synchronize # noqa: F401 +except ImportError: + # Fallback to Threads on platforms that do not support semaphores + # https://github.com/pipxproject/pipx/issues/229 + from multiprocessing.dummy import Pool +else: + from multiprocessing import Pool + import userpath # type: ignore from pipx import constants from pipx.colors import bold, red @@ -574,7 +586,7 @@ def list_packages(venv_container: VenvContainer): venv_container.verify_shared_libs() - with multiprocessing.Pool() as p: + with Pool() as p: for package_summary in p.map(_get_package_summary, dirs): print(package_summary)