Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k 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 Reply

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...