flask_manager
drunc.utils.flask_manager
Flask application manager utilities for DRUNC.
Classes
CannotStartFlaskManager(message='An error occurred in Drunc.', grpc_error_code=None, details=None, **detail_kwargs)
Bases: DruncCommandException
Exception raised when the Flask manager cannot start.
Source code in drunc/exceptions.py
FlaskManager(name, app, port, workers=1, host='0.0.0.0')
Bases: Thread
Manager for Flask applications running in a separate thread.
It allows to have a Flask server under a thread, start and stop it. Note that it creates another endpoint accessible at the route /readystatus. This is used to poll if the service is up, however the user can provide it.
To use this code, one can use the following example:
from flask import Flask
from flask_restful import Api
app = Flask('some-name')
api = Api(app)
api.add_resource(
AnEndpointResourceClass, "/endpoint",
)
from flask_manager import FlaskManager
manager = FlaskManager(
port = port,
app = app,
name = "some-name"
)
manager.start()
while not manager.is_ready():
from time import sleep
sleep(0.1)
Then, later on, to stop it:
Initialize a FlaskManager.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the Flask manager. |
required |
app
|
Flask
|
The Flask application to manage. |
required |
port
|
int
|
The port to run the Flask server on. |
required |
workers
|
int
|
The number of Gunicorn workers. Defaults to 1. |
1
|
host
|
str
|
The host address to bind to. Defaults to "0.0.0.0". |
'0.0.0.0'
|
Source code in drunc/utils/flask_manager.py
Methods:
__del__()
is_ready()
Check if the Flask manager is ready to serve requests.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if ready, False otherwise. |
is_terminated()
Check if the Flask manager has been terminated.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if terminated, False otherwise. |
restart_renew()
Restart and renew the Flask manager.
Stops the current instance and creates a new one with the same configuration.
Returns:
| Name | Type | Description |
|---|---|---|
FlaskManager |
FlaskManager
|
A new FlaskManager instance with the same settings. |
Source code in drunc/utils/flask_manager.py
run()
stop()
Stop the Flask manager and terminate the Gunicorn process.
Sends SIGTERM to the Gunicorn process and joins the Flask process thread.
Source code in drunc/utils/flask_manager.py
GunicornStandaloneApplication(app, options=None)
Bases: BaseApplication
Standalone Gunicorn application wrapper.
Initialize a GunicornStandaloneApplication.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
app
|
Flask
|
The Flask application to run. |
required |
options
|
dict[str, object] | None
|
Configuration options for Gunicorn. Defaults to None. |
None
|
Source code in drunc/utils/flask_manager.py
Methods:
load()
load_config()
Load Gunicorn configuration from options.
Source code in drunc/utils/flask_manager.py
Functions:
main()
Main entry point for demonstrating the FlaskManager.
Creates a simple Flask application with a dummy endpoint and starts it.