def register_extensions(app): """ 扩展实例化 :param app: :return: """ db.init_app(app) moment.init_app(app) migrate.init_app(app=app, db=db) cache.init_app(app) login_manager.init_app(app) # 登录过滤保护 login_manager.exempt_views((user_bp, demo_bp, audio_bp, img_api_bp, ocr_api_bp)) session.init_app(app) csrf.init_app(app) # csrf过滤保护 csrf.exempt_views((demo_bp, audio_bp, img_api_bp, ocr_api_bp)) # 定时任务 解决FLASK DEBUG模式定时任务执行两次 if os.environ.get('FLASK_DEBUG', '0') == '0': scheduler.init_app(app) scheduler.start() elif os.environ.get('WERKZEUG_RUN_MAIN') == 'true': scheduler.init_app(app) scheduler.start()
def configure_scheduler(app): logging.basicConfig() scheduler.add_job(auto_feeding, 'interval', kwargs={'app': app}, seconds=10) scheduler.start()
def register_extensions(app): """Register Flask extensions.""" db.init_app(app) migrate.init_app(app, db) login_manager.init_app(app) bcrypt.init_app(app) if not scheduler.running: scheduler.init_app(app) scheduler.start() jwt.init_app(app)
import atexit import asyncio from extensions import scheduler def delete_last_location_and_health_periodically(): print(time.strftime("%A, %d. %B %Y %I:%M:%S %p")) db.session.query(UserHealth).delete() db.session.query(LastLocationPostGis).delete() db.session.commit() print('deleted all records of userhealth and lastlocationpostgis') scheduler.add_job(func=delete_last_location_and_health_periodically, trigger="interval", seconds=5) scheduler.start() atexit.register(lambda: scheduler.shutdown()) @app.route('/sendregistrationdata', methods=['POST', 'GET']) def send_registration_data(): if request.method == 'POST': data = request.get_json(force=True) try: email = data['email'] first_name = data['first_name'] last_name = data['last_name'] phone_number = data['phone_number'] # hashed_password = bcrypt.hashpw(data['password'].encode('utf-8'), # bcrypt.gensalt()) password = data['password']