Created
January 31, 2023 18:55
-
-
Save christinebuckler/351bfe0547bcaf6f1524fbb32467aea5 to your computer and use it in GitHub Desktop.
get number of cores and memory from OS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import multiprocessing | |
# get the number of logical cpu cores | |
n_cores = os.cpu_count() | |
n_cores = multiprocessing.cpu_count() | |
# get total physical memory | |
# https://stackoverflow.com/questions/22102999/get-total-physical-memory-in-python | |
# For MacOS, as per user reports, this works with Python 3.7 but not with Python 3.8 | |
mem_bytes = os.sysconf('SC_PAGE_SIZE') * os.sysconf('SC_PHYS_PAGES') | |
mem_gib = mem_bytes/(1024.**3) # e.g. 3.74 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment