Skip to content

Commit

Permalink
examples/deepzoom: parallelize deepzoom_tile to available CPUs
Browse files Browse the repository at this point in the history
On Python 3.13+, and on all Python versions on Linux, set the default job
count to the number of available CPUs.  Otherwise, default to 4 jobs as
before.

Signed-off-by: Benjamin Gilbert <[email protected]>
  • Loading branch information
bgilbert committed Oct 28, 2024
1 parent 803b409 commit 72300fe
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions examples/deepzoom/deepzoom_tile.py
Original file line number Diff line number Diff line change
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

0 comments on commit 72300fe

Please sign in to comment.