1

i have some that should not execute again when reload flask it should be work only once even reload flash

from flask import *
from wxpy import *

app = Flask(__name__)
bot = None


@app.route('/')
def hello_world():
    return 'hi,my hello ,iiiHello wxpy! not reload bot'

@app.route('/hello/')
def hello():
    return "hello"

@app.route('/friends/')
def hellos():
    guys = bot.friends()
    return render_template('hello.html', guys=guys)


def init_bot():
    bot = Bot(console_qr = 1)
    return bot

if __name__ == '__main__':
    bot = init_bot()
    app.run(debug=True,use_reloader=False)

i just use wxpy and flask to write webs i want to reload flask auto but i do not want to execute the Bot init funciton again,for a i need to scan qr-code any one can help me i can modiy code and take effect immediately but do not need to scan qr-code again

cnboy
  • 21
  • 3
  • In the init_bot function save the init status create a file and run the init_bot on condition that the file doesn't exist. – Arunmozhi Oct 18 '17 at 12:08

1 Answers1

0

I am expanding my comment to an answer.

Create a file to flag that bot has been initiated.

import os.path

def init_bot():
    bot = Bot(console_qr = 1)
    # create an empty file to denote that bot has been initiated 
    open('bot.initiated', 'a').close()  
    return bot

if __name__ == '__main__':
    if not os.path.exits('bot.initiated'):
        bot = init_bot()
    app.run(debug=True)

Caveat:

This works only for situation where you are developing and would like to prevent some action during reloads. If you really want to stop the dev-server and initialise the app from the beginning including init_bot, then you should remove the dummy file bot.initiated manually from your system.

Arunmozhi
  • 1,034
  • 8
  • 17
  • i t seems your code work properly i have got anther answer,just cache the wechat login info. i got my first answer from stackoverflow from you ,than you – cnboy Oct 21 '17 at 02:01