Forums

SQLALCHEMY_POOL_RECYCLE

I cant figure out what I am missing. mysql times out. I have tried to set SQLALCHEMY_POOL_RECYCLE but I am obvious missing something. I use Flask and Sqlalchemy. are from My .env file

I am an paid subscribtion!

From my app/init.py :

db = SQLAlchemy()
migrate = Migrate()
login = LoginManager()
login.login_view = 'auth.login'
login.login_message = _l('Du skal logge ind for at se denne side.')
mail = Mail()
bootstrap = Bootstrap5()
moment = Moment()
babel = Babel()
csrf = CSRFProtect()

def create_app(config_class=Config):
    app = Flask(__name__)
    app.config.from_object(config_class)
    CORS(app)
    db.init_app(app)
    app.config["SQLALCHEMY_POOL_RECYCLE"] = 280
    app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
    migrate.init_app(app, db)
    login.init_app(app)

From my config.py :

import os
from dotenv import load_dotenv

basedir = os.path.abspath(os.path.dirname(__file__))
load_dotenv(os.path.join(basedir, '.env'))

class Config(object):
    SECRET_KEY = os.environ.get('SECRET_KEY') or 'you-will-never-guess'
    SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URI')
    SQLALCHEMY_POOL_RECYCLE = 280
    SQLALCHEMY_TRACK_MODIFICATIONS = False
    LOG_TO_STDOUT = os.environ.get('LOG_TO_STDOUT')
    MAIL_SERVER = os.environ.get('MAIL_SERVER')
    MAIL_PORT = int(os.environ.get('MAIL_PORT') or 25)
    MAIL_USE_TLS = os.environ.get('MAIL_USE_TLS') is not None
    MAIL_USERNAME = os.environ.get('MAIL_USERNAME')
    #SENDGRID_API_KEY=os.environ.get('SENDGRID_API_KEY')
    MAIL_DEFAULT_SENDER=os.environ.get('MAIL_DEFAULT_SENDER')
    MAIL_PASSWORD = os.environ.get('SENDGRID_API_KEY')
    ADMINS = ['sba@ainfo.dk']
    LANGUAGES = ['en', 'da']
    MS_TRANSLATOR_KEY = os.environ.get('MS_TRANSLATOR_KEY')
    ELASTICSEARCH_URL = os.environ.get('ELASTICSEARCH_URL')
    REDIS_URL = os.environ.get('REDIS_URL') or 'redis://'
    POSTS_PER_PAGE = 25
    UPLOAD_FOLDER = os.path.join(os.path.join(basedir, 'app'), 'static/uploads')
    UPLOAD_FOLDER_STATIC = ('uploads')
    UPLOAD_EXTENSIONS = ('.pdf', '.kmz')

What is the actual error that you get?

.

sqlalchemy.exc.OperationalError: (MySQLdb.OperationalError) (2013, 'Lost connection to MySQL server during query')
2023-03-08 08:09:10,798: [SQL: SELECT user.id AS user_id, user.username AS user_username, user.email AS user_email, user.password_hash AS user_password_hash, user.about_me AS user_about_me, user.last_seen AS user_last_seen, user.token AS user_token, user.token_expiration AS user_token_expiration, user.last_message_read_time AS user_last_message_read_time 
2023-03-08 08:09:10,798: FROM user 
2023-03-08 08:09:10,798: WHERE user.id = %s]

[edit by admin: formatting]

I think that you need to move those two app.config lines above the db.init_app call -- I believe that it will copy all of the DB configuration from the config at the time that the init is called, so if you set them afterwards it won't see them.

I have tried to move those two lines. It dosn't help. I still get "MySQLdb.OperationalError: (2013, 'Lost connection to MySQL server during query')". When I look at a copy of the app running on my local machine it looks as if the connection pool is set correct.

I have no idea what to do

After the line db.init_app(app), could you add the line:

print("App config", db.app.config['SQLALCHEMY_POOL_RECYCLE'], flush=True)

I have added the line:

print("App config", db.app.config['SQLALCHEMY_POOL_RECYCLE'], flush=True) after db.init_app(app)

it gives: "Something went wrong :-( Something went wrong while trying to load this website; please try again later.

If it is your site, you should check your logs to determine what the problem is."

and this in the errorlog:

2023-03-09 17:27:16,007: Error running WSGI application 2023-03-09 17:27:16,008: Error running WSGI application 2023-03-09 17:27:16,024: AttributeError: app 2023-03-09 17:27:16,024: File "/var/www/www_o-service_me_wsgi.py", line 111, in <module> 2023-03-09 17:27:16,025: from microblog import app as application # noqa 2023-03-09 17:27:16,025: 2023-03-09 17:27:16,025: File "/home/viborgoservice/o-service/microblog.py", line 6, in <module> 2023-03-09 17:27:16,025: app = create_app() 2023-03-09 17:27:16,025: 2023-03-09 17:27:16,025: File "/home/viborgoservice/o-service/app/init.py", line 40, in create_app 2023-03-09 17:27:16,025: print("App config", db.app.config['SQLALCHEMY_POOL_RECYCLE'], flush=True) 2023-03-09 17:27:16,025: 2023-03-09 17:27:16,026: File "/home/viborgoservice/.virtualenvs/oservice/lib/python3.10/site-packages/flask_sqlalchemy/extension.py", line 988, in getattr 2023-03-09 17:27:16,026: raise AttributeError(name) 2023-03-09 17:27:16,026: ********* 2023-03-09 17:27:16,026: If you're seeing an import error and don't know why, 2023-03-09 17:27:16,026: we have a dedicated help page to help you debug: 2023-03-09 17:27:16,026: https://help.pythonanywhere.com/pages/DebuggingImportError/

It looks like SQLALCHEMY_POOL_RECYCLE was deprecated and removed from flask-sqlalchemy in version 3. What version of flask-sqlalchemy are you using?

I don't know. but I have installede it primo january 2023. Can I downgrade? and what version?

It looks like you will need to downgrade to a version less than or equal to 2.4

I have to try a little more but this seems to do the trick:

app.config['SQLALCHEMY_ENGINE_OPTIONS'] = {'pool_size' : 100, 'pool_recycle' : 280}

I think you should update the help documents about this

t did it. app.config['SQLALCHEMY_ENGINE_OPTIONS'] = {'pool_size' : 100, 'pool_recycle' : 280}

Excellent, thanks for letting us know! Which of our documentation were you using?

In a related thread on our US forums someone pointed out that the page at https://help.pythonanywhere.com/pages/UsingSQLAlchemywithMySQL/ needed updating, so we've fixed that. And, of course, our blog post on getting started with Flask and MySQL will need to be updated too.