Last active
November 3, 2019 03:07
-
-
Save Sanix-Darker/458d880381cc4aee0b5bdc048167c059 to your computer and use it in GitHub Desktop.
[PYTHON]LIMIT CPU USAGE, yes it's possible, to optimise the whole program.
This file contains hidden or 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 signal | |
| import resource | |
| import os | |
| import random | |
| # To Limit CPU time | |
| def time_exceeded(signo, frame): | |
| print("CPU exceeded...") | |
| raise SystemExit(1) | |
| def set_max_runtime(seconds): # Install the signal handler and set a resource limit | |
| soft, hard = resource.getrlimit(resource.RLIMIT_CPU) | |
| resource.setrlimit(resource.RLIMIT_CPU, (seconds, hard)) | |
| signal.signal(signal.SIGXCPU, time_exceeded) # To limit memory usage | |
| def set_max_memory(size): | |
| soft, hard = resource.getrlimit(resource.RLIMIT_AS) | |
| resource.setrlimit(resource.RLIMIT_AS, (size, hard)) | |
| set_max_runtime(100) | |
| # set_max_memory(9000) | |
| value = 0 | |
| # Test it | |
| while True: | |
| time.sleep(2) | |
| for x in range(10): | |
| rd_generated = random.randint(1,99999999) | |
| value += rd_generated | |
| print ("> Random generated : {}".format(rd_generated)) | |
| print ("> Value: {}".format(value)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment