Skip to content

Instantly share code, notes, and snippets.

@stugmi
Created January 21, 2023 02:53
Show Gist options
  • Select an option

  • Save stugmi/5c7470c961107ddf46ed2d2928afff84 to your computer and use it in GitHub Desktop.

Select an option

Save stugmi/5c7470c961107ddf46ed2d2928afff84 to your computer and use it in GitHub Desktop.
print core list with option to exclude hyperthread cores
from subprocess import Popen, PIPE
def lscpu(show_hyperthreads: bool = True) -> list:
cmd = "lscpu -a -e".split()
stdout, stderr = Popen(cmd, stdout=PIPE).communicate()
out = stdout.decode().split("\n")
header = out[0].split()
cores = [dict(zip(header, row.split())) for row in out[1:-1]]
if not show_hyperthreads:
return cores[::2] # every other
return cores
if __name__ == "__main__":
from rich import print # pip install rich
cores = lscpu(show_hyperthreads=False)
print(cores)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment