Jayant
Jayant

Reputation: 83

Is it possible to change code of flask without rerunning the flask server after deployment?

Consider, I have a flask server deployed which routes to multiple webpages. If I want to change content of one route, by changing its code, is it possible to reflect those changes in webpages without rerunning the flask server? It is possible to host and rerun other scripts on the linux server or entriely another flask server as long as the website url(port number and route) doesn't change.

Please suggest any way you can come up with!

Upvotes: 6

Views: 3764

Answers (5)

marco
marco

Reputation: 41

I think you can only do this with the Apache Webserver. Refer to the Flask documentation. I haven't tried it (yet), but when you have deployed your new code any small change in the wsgi-file should automatically reload the code.

You mentioned two requirements

  1. I have a flask server deployed I would assume you mean it is deployed in a production environment. This automatically rules out debug-mode from the development server, because it is not stable, efficient or secure for production use.
  2. Without rerunning the flask server This rules out autoreload, because this will completely restart your server. Any requests that come in until the server is ready again will fail.

Upvotes: 0

Nilanka Manoj
Nilanka Manoj

Reputation: 3728

setting flask environment will do the thing for you in the shell.

export FLASK_ENV=development

Upvotes: 5

anils
anils

Reputation: 1962

Assuming you are using flask development server,

Yes, its possible using use_reloader=True,

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    # You business logic code
    return some_data

if __name__ == '__main__':
    app.run(debug=False, host='0.0.0.0', port=8000,
            use_reloader=True, threaded=True)


However it is not good idea to use it in production, related Is the server bundled with Flask safe to use in production?

Upvotes: 2

One way of going about is by running your application with debug mode ON through app.run(debug=True)

The problem with this approach is that your application would be exposed over the internet with the application's internal debugger on which shouldn't be done.

A better way I can think of is to call the functions you need to change frequently from a different file other than where your core flask code exists and change in that file whenever needed. This way you don't need to change your Flask code and you won't need to restart the application


Edit:

A sample Flask route would be

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    # Route's business logic usually goes here
    return data

Now, instead of writing your application's business logic in the same file as your route, you could simply write it in a different file as follows and import it in the file with the route:

File with the business logic

def process_hello_world(params):
    # Write your business logic here which you want to change without reloading the application
    return data

And in the application file with the route call the business logic as follows:

from flask import Flask
from 
app = Flask(__name__)

@app.route('/')
def hello_world():
    data = process_hello_world()
    return data

Upvotes: -1

pastaleg
pastaleg

Reputation: 1838

My assumption is that you want your deployment service to recognize that a change has occurred in a file, which prompts it to automatically restart to reflect the changes, rather than you manually restarting the service.

Assuming that you are deploying on a production uWSGI server, starting the service with touch-reload=/path/to/folder would allow uWSGI to reload when the specified file/folder is modified or touched. https://uwsgi-docs.readthedocs.io/en/latest/Options.html#touch-reload

--touch-reload=/path/to/file.txt

Upvotes: 0

Related Questions