我试图了解如何在Mac OS X中的该过程中对进程进行资源限制(我有10.6).在CentOS上,以下测试程序(python3.3)按照我的预期工作:
import resource
import signal
def confirm(prompt, instructions='(y)es or (n)o only, please.'):
while True:
ok = input(prompt)
if ok in ('y', 'ye', 'yes'):
return True
if ok in ('n', 'no'):
return False
print(instructions)
def handle_cpu_soft_limit(signal, frame):
print('CPU soft limit reached.')
if not confirm('Do you want to continue anyway? (y/n): '):
raise ResourceWarning()
def long_function():
a = 0
while True:
a += 1
a -= 1
def main():
# set soft limit to 1 second, hard limit to 5 …Run Code Online (Sandbox Code Playgroud)