Welcome to ZaiZheLe Developer Zone-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
471 views
in Technique[技术] by (71.8m points)

python - What is the best way to run a web app using gunicorn at certain hours of the day?

I have a little dashboard that runs with python - dash and I've already deployed it successfully in production using GUNICORN.

However, I only want to run it on productive hours (say 8:00 to 20:00). What is the best way to do it? Using crontab to run the GUNICORN launching line? Using crontab to kill the GUNICORN process at the end of the day after launching it with noHup?

Thanks!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

One possible approach would be to add the logic directly in you Dash app, i.e. something like

import dash
import dash_html_components as html
import datetime

def the_actual_layout():
    return html.Div("Hello world!")

def layout():
    hour = datetime.datetime.now().hour
    # If we are outside business hours, return an error message.
    if(hour < 8 or hour > 19):
        return html.Div("The app is only available between 08:00 and 20:00, please come back tomorrow.")
    # Otherwise, render the app.
    return the_actual_layout()

app = dash.Dash()
app.layout = layout

if __name__ == '__main__':
    app.run_server()    

which would thus eliminate the need for external tools to start/stop the app.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to ZaiZheLe Developer Zone-Open, Learning and Share

548k questions

547k answers

4 comments

40.7k users

...