Skip to content

Instantly share code, notes, and snippets.

@bparaj
Created May 6, 2020 03:46
Show Gist options
  • Save bparaj/755e635613f872453abe2dde78182ce1 to your computer and use it in GitHub Desktop.
Save bparaj/755e635613f872453abe2dde78182ce1 to your computer and use it in GitHub Desktop.
Count number of physical cpu cores to determine the number of parallel processes when using multiprocessing module in Python
import re
# Assume a system where /proc/cpuinfo exists. We will just parse this file with re module.
cpu_info = open("/proc/cpuinfo").read()
# Pattern of the line in "/proc/cpuinfo" which has the needed count:
# cpu cores : 12
patc = re.compile(r"cpu cores\s:\s(\d+)")
# Search and extract.
mo = patc.search(cpu_info)
core_count = int(mo.group(1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment