#!/usr/bin/env python # encoding: utf-8 # # Copyright (c) 2009 Doug Hellmann All rights reserved. # """ """ #end_pymotw_header import resource import sys import signal import time # Set up a signal handler to notify us # when we run out of time. def time_expired(n, stack): print 'EXPIRED :', time.ctime() raise SystemExit('(time ran out)') signal.signal(signal.SIGXCPU, time_expired) # Adjust the CPU time limit soft, hard = resource.getrlimit(resource.RLIMIT_CPU) print 'Soft limit starts as :', soft resource.setrlimit(resource.RLIMIT_CPU, (1, hard)) soft, hard = resource.getrlimit(resource.RLIMIT_CPU) print 'Soft limit changed to :', soft print # Consume some CPU time in a pointless exercise print 'Starting:', time.ctime() for i in range(200000): for i in range(200000): v = i * i # We should never make it this far print 'Exiting :', time.ctime()