Allows to temporarily modify the power management settings on a MacOS to run processes uninterruptedly.
Project description
espressomaker
espressomaker
is a Python 3 module that provides a context manager, among other functionalities, to modify the power management settings on a MacOS X system so that lengthy tasks (e.g. a machine learning training algorithm) can run uninterruptedly — without your Mac going to sleep.
More specifically, espressomaker
is a wrapper of caffeinate
, a shell command in MacOS X distributions that allows users to alter the system's sleep behavior. In this sense, espressomaker
runs caffeinate
subprocesses from the Python 3 interpreter or the IPython kernel from where it was imported and allows to control your Mac's sleep settings through a simple and intuitive set of Python commands.
Contents
1. Quick Start
To install espressomaker
, run the following on your Terminal:
$ pip install espressomaker
To use espressomaker
as a context manager for a block of code, run on a Python 3 interpreter or an IPython kernel:
from espressomaker import Espresso
with Espresso.shot():
function_1()
function_2()
...
The indented code will be run using the context manager of espressomaker
, Espresso.shot()
. While this code is running, your Mac won't go to sleep.
2. Purpose
espressomaker
provides a Python 3 module that prevents your Mac from sleep when you are running lengthy tasks — blocks of code that take a long time to finish.
Many applications that run on Python may take hours to finish, like machine learning training algorithms. If a task is actively running on a Python 3 interpreter — e.g. a Python script — or an iPython kernel — e.g. a Jupyter notebook — and the system goes to sleep, the running processes will be interrupted and all the progress related to that block of code will be lost.
To avoid that, espressomaker
provides a handful of functionalities, including a useful context manager to run blocks of code. The context manager functionality, provided in Espresso
— a module of espressomaker
—, will allow you to temporarily change the power management settings of your Mac while the indented block of code is running. Once the task is done, the settings will return to its default state.
espressomaker
is a package that intends to facilitate dealing with lengthy Python tasks such that the user can, in a single line of code, forget about dealing with interrupted processes.
3. Installation
To install espressomaker
, run on your terminal:
$ pip install espressomaker
You can find the package's PyPI link here.
Troubleshooting
The installation process using pip
should be uneventful. After the installation, the package should be located at:
/Users/<your_username>/.local/lib/pythonX.Y/site-packages/
, if you usepip
as the default package manager; or,/Users/<your_username>/anaconda3/lib/pythonX.Y/site-packages/
, if you useconda
as a package manager;
where X.Y is your current Python version (root environment). You can check if these directories are considered by Python's system's path by running:
import sys
sys.path
However, if when importing a ModuleNotFoundError
occurs, it could be possible that your current interpreter/kernel is not including the directory where espressomaker
is installed at. Although this is unlikely, you can find the current location of the package by running on your Terminal:
$ find /Users/ -type d -name 'espressomaker' 2>/dev/null | grep ".*python.*"
The previous command will search for a folder called espressomaker
in the Users/
directory and only print the matches that belong to a python
subdirectory. If the directory found is not on sys.path
, you can manually add it to Python's path using:
sys.path.append('<path>')
4. User guide
4.1 Working principle
The Espresso
module from espressomaker
allows you to run caffeinate
subprocesses — child processes of your current Python interpreter or IPython kernel. caffeinate
is a shell command available on MacOS distributions that allows to modify the power management settings of your system by creating assertions. In this context, caffeinate
is used to prevent your MacOS system from sleeping while a task is being computed.
The Espresso
module offers two ways to run caffeinate
subprocesses:
- As a context manager for a task — a block of code —, using the
shot()
method, or; - As a manual method call, using the
opentab()
andclosetab()
methods (i.e. the user defines when to start running the subprocess and when to finish it).
In either way, your Mac will not sleep until the task is completed — when using the context manager mode — or until you manually close the tab.
4.2 Importing the module
To import the functionalities of espressomaker
to Python, run:
from espressomaker import Espresso
4.3 Default settings
The Espresso
module has two class-level settings: verbose
and display_on
. The verbose
parameter enables messages related to the status of the module when using the shot()
context manager. The display_on
parameter determines whether the display of your Mac will remain on (if display_on = True
) or if it will turn off (display_on = False
) as per the current settings of your Mac.
The default class-level settings can be retrieved using config()
:
>>> Espresso.config()
Espresso(verbose = True, display_on = False)
To change these class-level settings — to set new default settings —, just pass in the parameters you want to change into Espresso.config()
:
>>> Espresso.config(display_on = True)
Espresso(verbose = True, display_on = True)
Safety note
For safety reasons, espressomaker
only works when your Mac is connected to AC power — it will not work if you are using battery power.
4.4 Using the context manager — Espresso.shot()
One of the main advantages of the Espresso
module is that it allows to run a task — a block of code — using a context manager. The context manager enables the caffeinate
functionality — instantiates the subprocess — for the code inside it and then closes the process — kills the subprocess.
To use it, run:
>>> with Espresso.shot(display_on = True):
... function_1()
... function_2()
...
As shown above, you can always override the display_on
default settings by passing in a new value for that argument, which will only work for that instance call.
4.5 Manually opening and closing tabs — Espresso.opentab()
and Espresso.closetab()
Espresso
also provides a manual way to instantiate a "caffeinate" subprocess in the current interpreter or kernel. The opentab()
and closetab()
methods allow you to instantiate and kill the caffeinate
subprocess, respectively.
>>> Espresso.opentab()
[espressomaker] Espresso tab opened on Mon, 23/Sep/2019 10:38:46 (display_on = False).
# Your work
>>> Espresso.closetab()
[espressomaker] Espresso tab closed.
The Espresso
module will prevent you from opening more than one caffeinate
subprocess for the same parent process — e.g. the Python interpreter, the IPython kernel from which you are running espressomaker
. Moreover, you can always run espressomaker
in multiple interpreters and kernels and check which caffeinate
subprocess belongs to your current interpreter or kernel by running Espresso.check()
.
Warning
While opening more than one caffeinate
subprocess from a single parent process using espressomaker
is not possible, if it occurs you might not be able to use closetab()
to close all the running subprocesses. When you kill the parent process — e.g. close the Jupyter notebook, restart the kernel — all the child processes are killed along with it. If for some reason you suspect a caffeinate
process is still running, you can try to pinpoint it using Espresso.check()
, or you can kill all the caffeinate
processes in your Mac running Espresso.killall()
.
4.6 Checking the tabs — Espresso.check()
Espresso.check()
allows you to retrieve a list of all the running caffeinate
processes in your Mac. If you have one running in your current interpreter or kernel, it will be explicitly indicated:
>>> Espresso.check()
[espressomaker] The following "caffeinate" processes were found:
USER PID COMMAND
<your_username> 62900 caffeinate -is -w 5531 (This kernel)
4.7 Killing all caffeinate
processes — Espresso.killall()
The killall()
method will kill all caffeinate
processes running in the system. Before running it, be sure that you don't have other caffeinate
active processes that you might need.
Formatting (GitHub flavor) passed and completed.
Release History
v0.1a1
Basic skeleton of the package ready for shipping to TestPyPI.
v0.1a2
- Automated file exporting from .ipynb to .py and standardized the formatting.
- Automated file exporting from .ipynb to .md and standardized the formatting.
- Improved variable handling on instance methods.
- Added a message for the user to recognize the current kernel when using opentabs().
- Added debugging tracers for all private methods.
- Finished class- and static- methods docstrings.
- Updated setup.py.
v0.1b1
- Improved HISTORY.md title formatting.
- Updated "classifiers" of setup.py.
- Changed opentabs() classmethod to check() in espresso.py.
- Successfully ran manual tests in all APIs.
v0.1b2
- Added PyPI version and GitHub issues badges to README.md.
- Ran installation test using
$ pip install espressomaker
. - Added config() classmethod to allow user modify Espresso class-level settings. Returns current settings.
- Added parameters to shot() and opentab() to allow user override "display_on" class-level setting.
- Repositioned status retrieval in closetab() classmethod.
- Added return message for killall() staticmethod.
- Added atexit.register call to closetab() (to be used when using opentab() in a .py script and not using closetab() at the end; however, killing the parent process should kill the "caffeinate" subprocess anyway).
- Finished User Guide in README.md.
v0.1rc1
- Corrected numbering and hrefs on README.md to work correctly on GitHub.
TODO
- Finish unittest.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Hashes for espressomaker-0.1rc1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 51f779136b7c28ee18fa0201f0172819eeddeb7dc4113c3bfa34521e4af4dd18 |
|
MD5 | 9e830ba1a8b12397879b9206564fd55b |
|
BLAKE2b-256 | 1751d5b60d50016601688de5a372f90814fb6e4df4ac74bdeefd896687d7583e |