Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import psutil
- import sys
- def check_resources(limit_cpu_percent, limit_ram_mb):
- pid = os.getpid()
- py = psutil.Process(pid)
- cpu_usage = py.cpu_percent()
- ram_usage = py.memory_info()[0] / 2. ** 20 # Convert bytes to MB
- if cpu_usage > limit_cpu_percent or ram_usage > limit_ram_mb:
- print('CPU or Memory limit exceeded...stopping')
- sys.exit()
- # Use it in your code like this
- while True: # or your main loop
- # your code here
- check_resources(limit_cpu_percent=80, limit_ram_mb=500)
Advertisement
Comments
-
- cmd: pip install psutil
-
- You Kill server
-
- Stop run
-
- You kill pc you
-
- No joke %100
-
- My illegal code
Add Comment
Please, Sign In to add comment
Advertisement