|
1 | 1 | # cpulimiter 🚀
|
2 | 2 |
|
3 |
| -A Python library for Windows to limit the CPU usage of running processes. |
| 3 | +[](https://pypi.org/project/cpulimiter/) |
| 4 | +[](https://pypi.org/project/cpulimiter/) |
| 5 | +[](https://github.com/Ahmed-Ashraf-dv/CPULimiter/blob/main/LICENSE) |
4 | 6 |
|
5 |
| -## Features ✨ |
| 7 | +A simple, lightweight Python library for Windows to limit the CPU usage of any running process. Reduce CPU load, save power, and prevent overheating. |
6 | 8 |
|
7 |
| -- 🎯 Limit process CPU usage by a specified percentage. |
8 |
| -- 🔍 Target processes by Process ID (PID), executable name, or window title. |
9 |
| -- 🤝 Manage and control multiple limited processes simultaneously. |
10 |
| -- 🛠️ Utility functions to discover running and active processes. |
| 9 | +## 🤔 Why cpulimiter? |
11 | 10 |
|
12 |
| -## Installation 📦 |
| 11 | +Have you ever had a program like `Google Chrome`, a game, or a background task consume 100% of your CPU, making your system unresponsive and your fans spin like a jet engine? |
| 12 | + |
| 13 | +`cpulimiter` solves this by throttling the application, allowing you to specify exactly how much CPU it can use. This is perfect for: |
| 14 | + |
| 15 | +- 🎮 Capping the CPU usage of games to prevent overheating. |
| 16 | +- 🌐 Limiting resource-hungry browsers like Chrome or Edge when running multiple tabs. |
| 17 | +- 💼 Running heavy data processing or video encoding tasks in the background without slowing down your machine. |
| 18 | +- 🔋 Saving battery life on laptops by reducing the power consumption of demanding applications. |
| 19 | +- 🤫 Quieting down noisy CPU fans. |
| 20 | + |
| 21 | + |
| 22 | +## ✨ Features |
| 23 | + |
| 24 | +- 🎯 **Limit CPU Usage:** Throttle process CPU usage to a specific percentage. |
| 25 | +- 🔍 **Flexible Targeting:** Target processes by Process ID (PID), executable name (`"chrome.exe"`), or even window title. |
| 26 | +- 🤝 **Multi-Process Management:** Control and limit multiple processes at the same time. |
| 27 | +- 🛠️ **Process Discovery:** Includes utility functions to find running applications and the active foreground window. |
| 28 | +- 🕊️ **Lightweight:** Has a minimal performance footprint and no external dependencies. |
| 29 | + |
| 30 | +## 📦 Installation |
13 | 31 |
|
14 | 32 | ```bash
|
15 | 33 | pip install cpulimiter
|
16 | 34 | ```
|
17 | 35 |
|
18 |
| -## Quick Start 📖 |
| 36 | +## 📖 Quick Start |
19 | 37 |
|
20 |
| -The following example demonstrates how to limit `chrome.exe` to 5% of CPU usage (a 95% limit). |
| 38 | +The following example limits all `chrome.exe` processes to just 5% of a single CPU core's power (a 95% limit). |
21 | 39 |
|
22 | 40 | ```python
|
23 | 41 | from cpulimiter import CpuLimiter
|
24 | 42 | import time
|
25 | 43 |
|
26 |
| -# 1. Initialize the limiter |
27 |
| -limiter = CpuLimiter() |
28 |
| - |
29 |
| -# 2. Add the process directly by its name. |
30 |
| -# The library will find the PID for "chrome.exe" for you. |
31 |
| -limiter.add(process_name="chrome.exe", limit_percentage=95) |
32 |
| - |
33 |
| -# 3. Start the limit |
34 |
| -# You can also start it by name. |
35 |
| -print("Limiting Chrome for 15 seconds...") |
36 |
| -limiter.start(process_name="chrome.exe") |
| 44 | +# 1. Find all "chrome.exe" processes and limit them to 5% CPU. |
| 45 | +# The limit is a percentage (0-100). 95 means "limit by 95%", so it can only use 5%. |
| 46 | +limiter = CpuLimiter({"chrome.exe": 95}) |
37 | 47 |
|
| 48 | +# 2. The limiter is now running in the background. |
| 49 | +# Let's keep it running for 15 seconds to see the effect. |
| 50 | +print("Limiting Chrome's CPU usage for 15 seconds...") |
38 | 51 | time.sleep(15)
|
39 | 52 |
|
40 |
| -# 4. Stop the limit |
41 |
| -print("Stopping limit.") |
42 |
| -limiter.stop(process_name="chrome.exe") |
43 |
| -print("Process limit removed.") |
| 53 | +# 3. To stop limiting, simply call the shutdown() method. |
| 54 | +limiter.shutdown() |
| 55 | +print("CPU limit removed. Chrome is back to normal.") |
44 | 56 | ```
|
| 57 | +*You can check your Task Manager to see the effect in real-time!* |
| 58 | + |
| 59 | +## ⚙️ How It Works |
45 | 60 |
|
46 |
| -## Examples 📚 |
| 61 | +`cpulimiter` works by rapidly suspending and resuming the threads of a target process. For example, to achieve a 50% CPU limit, the library suspends the process for 10 milliseconds and then resumes it for 10 milliseconds, effectively cutting its CPU time in half. This cycle is managed by a lightweight, high-precision background thread. |
47 | 62 |
|
48 |
| -Check out the `examples/` folder for different use cases: |
| 63 | +## 📚 Examples |
49 | 64 |
|
50 |
| -- **`basic_usage.py`** - Simple introduction to the library |
51 |
| -- **`simple_limit.py`** - Manually limit specific applications |
52 |
| -- **`cpu_saver.py`** - Automatic CPU saver that limits all background apps |
53 |
| -- **`advanced_interactive.py`** - Interactive mode with real-time control |
| 65 | +Check out the `examples/` folder for more advanced use cases: |
| 66 | + |
| 67 | +- **`basic_usage.py`** - A simple, manual introduction to the library's methods. |
| 68 | +- **`simple_limit.py`** - Manually limit a list of specific applications. |
| 69 | +- **`cpu_saver.py`** - An automatic CPU saver that throttles all applications that are not in the foreground. |
| 70 | +- **`advanced_interactive.py`** - An interactive command-line tool for real-time process management. |
54 | 71 |
|
55 | 72 | ## API Reference
|
56 | 73 |
|
@@ -91,4 +108,12 @@ Returns a dictionary containing the `pid`, `name`, and `title` of the foreground
|
91 | 108 |
|
92 | 109 | #### `get_active_app_pids()`
|
93 | 110 |
|
94 |
| -Returns a dictionary of all processes with visible windows. |
| 111 | +Returns a dictionary of all processes with visible windows, mapping their PIDs to their executable names. |
| 112 | + |
| 113 | +## 🤝 Contributing |
| 114 | + |
| 115 | +Contributions, issues, and feature requests are welcome! Feel free to check the [issues page](https://github.com/Ahmed-Ashraf-dv/CPULimiter/issues). |
| 116 | + |
| 117 | +## 📜 License |
| 118 | + |
| 119 | +This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. |
0 commit comments