Skip to content

Commit c8fde0c

Browse files
Handle errors gracefully (#311)
I do not expect `sysctl -n hw.ncpu` to fail, but you never know. Also use exclusive if because: * function `os.sysconf` is available on Unix only: https://docs.python.org/3/library/os.html#os.sysconf * environment variable `NUMBER_OF_PROCESSORS` is set on Windows only
1 parent f6869ab commit c8fde0c

1 file changed

Lines changed: 7 additions & 3 deletions

File tree

blosc/toplevel.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,14 @@ def detect_number_of_cores():
3535
if isinstance(ncpus, int) and ncpus > 0:
3636
return ncpus
3737
else: # OSX:
38-
return int(subprocess.run(("sysctl", "-n", "hw.ncpu"),
39-
stdout=subprocess.PIPE).stdout)
38+
completed = subprocess.run(("sysctl", "-n", "hw.ncpu"),
39+
stdout=subprocess.PIPE)
40+
if completed.returncode == 0:
41+
ncpus = int(completed.stdout)
42+
if ncpus > 0:
43+
return ncpus
4044
# Windows:
41-
if "NUMBER_OF_PROCESSORS" in os.environ:
45+
elif "NUMBER_OF_PROCESSORS" in os.environ:
4246
ncpus = int(os.environ["NUMBER_OF_PROCESSORS"])
4347
if ncpus > 0:
4448
return ncpus

0 commit comments

Comments
 (0)