User Guide#
Wakepy main Python API is are the wakepy Modes, which are states that are activated and deactivated and which keep your system awake. The method for activating the mode depends on your platform (among other things) and is determined by the used Method. For example keep.presenting mode is implemented by org.gnome.SessionManager for Linux with GNOME DE, SetThreadExecutionState for Windows and caffeinate for MacOS. In most cases, wakepy does nothing but calls an executable (caffeinate), a DLL function call (SetThreadExecutionState) or a D-Bus method (org.gnome.SessionManager). Wakepy helps in this by providing a coherent API which should just work™ on any system. Or, at least that is the vision of wakepy.
Entering a wakepy.Mode#
The wakepy modes are implemented as context managers of type wakepy.Mode. The available convenience wrappers for creating a Mode are keep.running() and keep.presenting(). These are used with the with statement:
from wakepy import keep
with keep.running():
# Do something that takes a long time. The system may start screensaver
# / screenlock or blank the screen, but CPU will keep running.
When entering the context, a Mode instance (m) is returned:
with keep.running() as m:
...
The Mode has following important attributes:
m.active:True, if entering mode was successful. Can be faked in CI.m.active_method: The name of the active method. Will beNoneafter mode is deactivated.m.used_method: The name of the used method. Will not be reset toNoneafter deactivation.m.activation_result: AnActivationResultinstance which gives more detailed information about the activation process.
Which wakepy Method was used?#
New in version 0.8.0.
When you would like to check how exactly did wakepy do what you asked it to,
you can check the used method from the Mode instance.
Example
from wakepy import keep
with keep.running() as m:
print('active_method:', m.active_method)
print('used_method:', m.used_method)
print('--------')
print('active_method:', m.active_method)
print('used_method:', m.used_method)
Example output:
active_method: org.gnome.SessionManager
used_method: org.gnome.SessionManager
--------
active_method: None
used_method: org.gnome.SessionManager
See also
Controlling the on-fail action#
New in version 0.8.0.
Changed in version 0.10.0: on_fail defaults to “warn” instead of “error”. See: wakepy/#376.
The wakepy Modes (e.g. keep.running and keep.presenting) also take an on_fail input argument which may be used to alter the behavior. Example:
from wakepy import keep
with keep.running(on_fail="warn"):
# do something
on-fail actions#
|
What happens? |
|---|---|
“warn” (default) |
Issues an |
“error” |
Raises an |
“pass” |
Does nothing |
Callable |
The callable is called with one argument: the result of the activation which is |
See also
Mode.__init__() on_fail argument.
Example: Notify user with a custom callable#
This is what you could do if you want to inform the user that the activation of the mode was not successful, but still want to continue to run the task:
from wakepy import keep, ActivationResult
def react_on_failure(result: ActivationResult):
print(f'Failed to keep system awake using {result.mode_name} mode')
def run_long_task():
print('Running a long task')
with keep.running(methods=[], on_fail=react_on_failure):
print('started')
run_long_task()
The
on_failparameter tokeep.running()is a callable which gets called with anActivationResultwhen the activation of the mode fails.Here we use empty list in
methodsto force failure
Example output:
Failed to keep system awake using keep.running mode
started
Running a long task
Example: Notify user and exit#
This is what you could do if you want to inform the user that the activation of the mode was not successful, and then exit from the with block:
from wakepy import keep, ActivationResult, ModeExit
def react_on_failure(result: ActivationResult):
print(f'Failed to keep system awake using {result.mode_name} mode')
def run_long_task():
print('Running a long task')
with keep.running(methods=[], on_fail=react_on_failure) as m:
print('started')
if not m.active:
print('exiting')
raise ModeExit
run_long_task()
The difference to the previous example is the
ModeExitwhich is used to exit the with block, if theMode.activeis notTrue.
Example output (notice that run_long_task was never called):
Failed to keep system awake using keep.running mode
started
exiting
Controlling the Methods to be tried#
New in version 0.8.0.
Wakepy tries in order a list of different Methods. By default this is of methods are all the Methods which implement the selected wakepy Mode. If you do not want to try all the methods, you can
Blacklist methods with the
omitparameterWhitelist methods with the
methodsparameter
Only either omit or methods may be given (not both).
Example: whitelisting methods#
This would try the methods called org.gnome.SessionManager and SomeOtherMethod, but never any other methods. Note that the order is not defined by the whitelist.
from wakepy import keep
with keep.running(methods=['org.gnome.SessionManager', 'SomeOtherMethod']):
...
Example: blacklisting methods#
This would never try the methods called org.gnome.SessionManager and SomeOtherMethod, but only any other methods implementing the selected Mode.
from wakepy import keep
with keep.running(omit=['org.gnome.SessionManager', 'SomeOtherMethod']):
...
See also
omit and methods parameter of Mode.from_name(), keep.running() and keep.presenting()
Controlling the order of Methods to be tried#
New in version 0.8.0.
To control the order of the methods to be tried, you may use the methods_priority argument. The argument should be a list of method names or sets of method names. Each item in the list is considered as “equal priority” and wakepy’s automatic logic will order the methods within the sets. An asterisk (*) may be used to denote “any other methods”.
Example: prioritizing methods#
This would put MethodA and MethodB to be in the highest priority group, and wakepy’s automatic prioritization logic would determine which one of the methods should be tried first. It would also put the MethodF to have the least priority and that method would only be tried after trying any other methods have failed.
from wakepy import keep
with keep.running(methods_priority=[{"MethodA", "MethodB"}, "*", "MethodF"]):
...
See also
methods_priority parameter of Mode.from_name(), keep.running() and keep.presenting()
experimental feature
The methods_priority is still an experimental feature and may change or be removed without further notice.