Skip to content

Commit

Permalink
Merge pull request #297 from bgilbert/cpu-count
Browse files Browse the repository at this point in the history
examples/deepzoom: parallelize deepzoom_tile to available CPUs
  • Loading branch information
bgilbert authored Oct 28, 2024
2 parents 9ded242 + 72300fe commit d2961cf
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
2 changes: 1 addition & 1 deletion examples/deepzoom/deepzoom_multiserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
if os.name == 'nt':
_dll_path = os.getenv('OPENSLIDE_PATH')
if _dll_path is not None:
with os.add_dll_directory(_dll_path): # type: ignore[attr-defined]
with os.add_dll_directory(_dll_path): # type: ignore[attr-defined,unused-ignore] # noqa: E501
import openslide
else:
import openslide
Expand Down
2 changes: 1 addition & 1 deletion examples/deepzoom/deepzoom_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
if os.name == 'nt':
_dll_path = os.getenv('OPENSLIDE_PATH')
if _dll_path is not None:
with os.add_dll_directory(_dll_path): # type: ignore[attr-defined]
with os.add_dll_directory(_dll_path): # type: ignore[attr-defined,unused-ignore] # noqa: E501
import openslide
else:
import openslide
Expand Down
19 changes: 16 additions & 3 deletions examples/deepzoom/deepzoom_tile.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
if os.name == 'nt':
_dll_path = os.getenv('OPENSLIDE_PATH')
if _dll_path is not None:
with os.add_dll_directory(_dll_path): # type: ignore[attr-defined]
with os.add_dll_directory(_dll_path): # type: ignore[attr-defined,unused-ignore] # noqa: E501
import openslide
else:
import openslide
Expand Down Expand Up @@ -382,6 +382,19 @@ def _shutdown(self) -> None:


if __name__ == '__main__':
try:
# Python 3.13+
available_cpus = os.process_cpu_count() # type: ignore[attr-defined]
except AttributeError:
try:
# Linux
available_cpus = len(
os.sched_getaffinity(0) # type: ignore[attr-defined,unused-ignore]
)
except AttributeError:
# default
available_cpus = 4

parser = ArgumentParser(usage='%(prog)s [options] <SLIDE>')
parser.add_argument(
'-B',
Expand Down Expand Up @@ -433,8 +446,8 @@ def _shutdown(self) -> None:
metavar='COUNT',
dest='workers',
type=int,
default=4,
help='number of worker processes to start [4]',
default=available_cpus,
help=f'number of worker processes to start [{available_cpus}]',
)
parser.add_argument(
'-o',
Expand Down
2 changes: 1 addition & 1 deletion tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
# environment.
_dll_path = os.getenv('OPENSLIDE_PATH')
if _dll_path is not None:
with os.add_dll_directory(_dll_path): # type: ignore[attr-defined]
with os.add_dll_directory(_dll_path): # type: ignore[attr-defined,unused-ignore] # noqa: E501
import openslide # noqa: F401 module-imported-but-unused


Expand Down

0 comments on commit d2961cf

Please sign in to comment.