Skip to content

Display the computer's OS and CPU core in Python

Last Updated on 2020-10-05 by Clay

python
Display Python code on the screen

Introduction

When we developing a Python project, sometimes we need to check which OS is user's current operating system, because of different operating systems we may have to use different methods to implement the same function.

In my recent project, I also need to know how many CPU cores the user has to implement a multi-threaded program, so, by the way, it is recorded here.

simply put, the platform module can be used to test the user operating system, and the cpu_count() function in multiprocessing module can be used to calculate the number of CPU cores.


Operating system

Checking the operating system is actually quite easy.

# -*- coding: utf-8 -*-
import platform

print(platform.system())


Output:

Windows

As you can see, my operating system is Windows and my other operating system is Linux.

Output:

Like this.

Each operating system returned should be as follows:

Linux:     Linux
Windows:   Windows
MacOS:     Darwin
JVM:       Java

CPU Cores

And then, how do we count the number of CPU cores?

from multiprocessing import cpu_count
print(cpu_count())


Output:

8

As you can see, my CPU has a total of 8 cores.


References


Read More

Tags:

Leave a Reply