The Alarm Extension provides easy access to setting an application alarm to handle timing out operations. See the Python Signal Library.
- No external dependencies.
- Only available on Unix/Linux
This extension does not honor any application configuration settings.
import time from cement.core.foundation import CementApp from cement.core.exc import CaughtSignal class MyApp(CementApp): class Meta: label = 'myapp' exit_on_close = True extensions = ['alarm'] with MyApp() as app: try: app.run() app.alarm.set(3, "The operation timed out after 3 seconds!") # do something that takes time to operate time.sleep(5) app.alarm.stop() except CaughtSignal as e: print(e.msg) app.exit_code = 1
$ python myapp.py ERROR: The operation timed out after 3 seconds! Caught signal 14
Lets the developer easily set and stop an alarm. If the alarm exceeds the given time it will raise
Set the application alarm to
timeseconds. If the time is exceeded
- time – The time in seconds to set the alarm to.
- msg – The message to display if the alarm is triggered.
Stop the application alarm.