-
Notifications
You must be signed in to change notification settings - Fork 581
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update presidio containers to use gunicorn #1497
base: main
Are you sure you want to change the base?
Conversation
/azp run |
Azure Pipelines successfully started running 1 pipeline(s). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great, thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great! Thanks :) I've added two minor points
|
||
[tool.poetry.extras] | ||
server = ["flask"] | ||
server = ["flask", "gunicorn"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add the gunicorn
license into the NOTICE file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated the NOTICE file, thanks
@@ -135,8 +135,9 @@ def supported_entities() -> Tuple[str, int]: | |||
def http_exception(e): | |||
return jsonify(error=e.description), e.code | |||
|
|||
server = Server() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I might be nit picking, but I'm curious on what is the difference between instantiating Server here compared to within the if name == main
. For instance, if I import app.py, the Server instance would be instantiated. I'm not sure if importing app.py is a use case, but it would be a side effect of this change in case somebody does this anywhere. WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thats a great pickup. When running with Gunicorn, it doesn't execute the code in the__main__
block. It just imports the module and looks for the app object to serve the application, which is why I moved the server code out of the __main__
block.
However, I didn't remove the __main__
block entirely, so if anyone wants to run just the flask development server they still have to ability to do so.
To avoid your concern of instantiating the server on import of app.py, we can avoid it by doing something like this:
def create_app():
server = Server()
return server.app
if __name__ == "__main__":
app = create_app()
port = int(os.environ.get("PORT", DEFAULT_PORT))
app.run(host="0.0.0.0", port=port)
We can then run Gunicorn in the Dockerfile like this:
CMD poetry run gunicorn -w $WORKERS -b 0.0.0.0:$PORT 'app:create_app()'
This should ensure that if anyone is just importing app.py it won't create the server instance. If you're happy with this, I'll commit the changes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! This is a better solution IMHO so let's go with it.
Change Description
presidio-analyzer/app.py
,presidio-anonymizer/app.py
, andpresidio-image-redactor/app.py
to use theapp
instance directly instead of creating a newServer
instance when running the application.PORT
andWORKERS
environment variables to the Dockerfiles for all 3 services, to allow configuration of the server port and number of worker processes.Issue reference
This PR fixes issue #1496
Checklist