Skip to content

Commit

Permalink
fallback to threads if no semaphore support (#277)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
justsh authored and gaborbernat committed Nov 29, 2019
1 parent 480faf3 commit 53718d9
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/pipx/commands/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import datetime
import hashlib
import logging
import multiprocessing
import shlex
import shutil
import subprocess
Expand All @@ -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
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 53718d9

Please sign in to comment.