Created
May 6, 2020 03:46
-
-
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
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 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