Skip to content

Instantly share code, notes, and snippets.

@christinebuckler
Created January 31, 2023 18:55
Show Gist options
  • Save christinebuckler/351bfe0547bcaf6f1524fbb32467aea5 to your computer and use it in GitHub Desktop.
Save christinebuckler/351bfe0547bcaf6f1524fbb32467aea5 to your computer and use it in GitHub Desktop.
get number of cores and memory from OS
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