Skip to content

Commit

Permalink
Fix issue with function nproc
Browse files Browse the repository at this point in the history
The function `nproc()` uses `os.sched_getaffinity()`, which is not available on every OS. It now uses `multiprocessing.cpu_count()` as fall-back.
  • Loading branch information
gmloose committed Dec 9, 2024
1 parent 3b08492 commit 5448b88
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions losoto/lib_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@ def nproc():
"""
Return the number of CPU cores _available_ to the current process, similar
to what the Linux `nproc` command does. This can be less than the total
number of CPU cores in the machine, which is returned by, e.g.,
`multiprocessing.cpu_count()`
number of CPU cores in the machine.
NOTE: This function uses `os.sched_getaffinity()`, which is not available
on every OS. Use `multiprocessing.cpu_count()` as fall-back.
"""
return len(os.sched_getaffinity(0))
try:
return len(os.sched_getaffinity(0))
except AttributeError:
return multiprocessing.cpu_count()


class multiprocManager(object):
Expand Down

0 comments on commit 5448b88

Please sign in to comment.