Skip to content

Commit d2961cf

Browse files
authored
Merge pull request #297 from bgilbert/cpu-count
examples/deepzoom: parallelize deepzoom_tile to available CPUs
2 parents 9ded242 + 72300fe commit d2961cf

File tree

4 files changed

+19
-6
lines changed

4 files changed

+19
-6
lines changed

examples/deepzoom/deepzoom_multiserver.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
if os.name == 'nt':
4343
_dll_path = os.getenv('OPENSLIDE_PATH')
4444
if _dll_path is not None:
45-
with os.add_dll_directory(_dll_path): # type: ignore[attr-defined]
45+
with os.add_dll_directory(_dll_path): # type: ignore[attr-defined,unused-ignore] # noqa: E501
4646
import openslide
4747
else:
4848
import openslide

examples/deepzoom/deepzoom_server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
if os.name == 'nt':
4343
_dll_path = os.getenv('OPENSLIDE_PATH')
4444
if _dll_path is not None:
45-
with os.add_dll_directory(_dll_path): # type: ignore[attr-defined]
45+
with os.add_dll_directory(_dll_path): # type: ignore[attr-defined,unused-ignore] # noqa: E501
4646
import openslide
4747
else:
4848
import openslide

examples/deepzoom/deepzoom_tile.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
if os.name == 'nt':
4949
_dll_path = os.getenv('OPENSLIDE_PATH')
5050
if _dll_path is not None:
51-
with os.add_dll_directory(_dll_path): # type: ignore[attr-defined]
51+
with os.add_dll_directory(_dll_path): # type: ignore[attr-defined,unused-ignore] # noqa: E501
5252
import openslide
5353
else:
5454
import openslide
@@ -382,6 +382,19 @@ def _shutdown(self) -> None:
382382

383383

384384
if __name__ == '__main__':
385+
try:
386+
# Python 3.13+
387+
available_cpus = os.process_cpu_count() # type: ignore[attr-defined]
388+
except AttributeError:
389+
try:
390+
# Linux
391+
available_cpus = len(
392+
os.sched_getaffinity(0) # type: ignore[attr-defined,unused-ignore]
393+
)
394+
except AttributeError:
395+
# default
396+
available_cpus = 4
397+
385398
parser = ArgumentParser(usage='%(prog)s [options] <SLIDE>')
386399
parser.add_argument(
387400
'-B',
@@ -433,8 +446,8 @@ def _shutdown(self) -> None:
433446
metavar='COUNT',
434447
dest='workers',
435448
type=int,
436-
default=4,
437-
help='number of worker processes to start [4]',
449+
default=available_cpus,
450+
help=f'number of worker processes to start [{available_cpus}]',
438451
)
439452
parser.add_argument(
440453
'-o',

tests/common.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
# environment.
3131
_dll_path = os.getenv('OPENSLIDE_PATH')
3232
if _dll_path is not None:
33-
with os.add_dll_directory(_dll_path): # type: ignore[attr-defined]
33+
with os.add_dll_directory(_dll_path): # type: ignore[attr-defined,unused-ignore] # noqa: E501
3434
import openslide # noqa: F401 module-imported-but-unused
3535

3636

0 commit comments

Comments
 (0)