Skip to content

Instantly share code, notes, and snippets.

@Sanix-Darker
Last active November 3, 2019 03:07
Show Gist options
  • Select an option

  • Save Sanix-Darker/458d880381cc4aee0b5bdc048167c059 to your computer and use it in GitHub Desktop.

Select an option

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.
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