quicken¶
Quicken is a Python package that enables CLI-based tools to start more quickly.
When added to a command-line tool, Quicken starts a server transparently the first time the tool is invoked. After the server is running, it is responsible for executing commands. This is fast because imports happen once at server start.
The library is transparent for users. Every time a command is run all context is sent to the server, including:
- arguments
- current working directory
- environment
- umask
- file descriptors for stdin/stdout/stderr
Usage:
Assume your application is my_app
and your original CLI entrypoint is my_app.cli.cli
. Create a file my_app/cli_wrapper.py
, with contents:
from quicken import cli_factory
@cli_factory('my_app')
def main():
# Import your existing command-line entrypoint.
# This is the expensive operation that only happens once.
from .cli import cli
# Return it.
return cli
Adapt setup.py
:
setup(
...
entry_points={
'console_scripts': ['my-command=my_app.cli_wrapper:cli']
},
...
)
If you have my_app/__main__.py
, it should look like:
from .cli_wrapper import main
main()
API Reference¶
The main interface
-
@
quicken.
cli_factory
(name, *, runtime_dir_path=None, log_file=None, server_idle_timeout=None, bypass_server=None, reload_server=None)[source]¶ Decorator to mark a function that provides the main script entry point.
To benefit most from the daemon speedup, you must do required imports within the factory function itself and then have the returned function itself do a minimal amount of configuration - only those things dependent on e.g. environment/cwd.
If any imported top-level modules make use of environment then they must be reconfigured on invocation of the cli, otherwise the environment of future clients will not be taken into account.
Parameters: - name (
str
) – the name used for the socket file. - runtime_dir_path (
Optional
[str
]) – the directory used for the socket and pid file. If not provided then we fall back to: $XDG_RUNTIME_DIR/quicken-{name} or $TMPDIR/quicken-{name}-{uid} or /tmp/quicken-{name}-{uid}. If the directory exists it must be owned by the current user and have permissions 700. - log_file (
Optional
[str
]) – optional log file used by the server, must be an absolute path. If not provided the default is $XDG_CACHE_HOME/quicken-{name}/server.log or $HOME/.cache/quicken-{name}/server.log. - server_idle_timeout (
Optional
[float
]) – time in seconds after which the server will shut down if no requests are being processed. - bypass_server (
Optional
[Callable
[[],bool
]]) – if True then run command directly instead of trying to use daemon. - reload_server (
Optional
[Callable
[[],bool
]]) – if True then restart the server before executing the function.
- Throws:
- QuickenError: If any directory used by runtime_dir does not have the
- correct permissions.
- name (