It is very common problem and been covered at the pooling doc. You may take 
a look 
at 
http://docs.sqlalchemy.org/en/latest/core/pooling.html#disconnect-handling-pessimistic

Personally, I don't use connection pool for celery task. Since the task is 
often long running and only need one connection to DB.

Best,
Rick

On Saturday, June 20, 2015 at 2:15:54 PM UTC+8, Rohitash Kumar wrote:
>
> My current setup include
>
> Flask, Flask-SQLAlchemy, celery
>
> Now the problem which I am running into is sometimes database queries 
> inside celery tasks gives stale data i.e If I ask for last record from 
> table I get second last while the last entry was inserted just 10-15 
> minutes before doing the query and sometimes I see exceptions like these.
>
> OperationalError("(OperationalError) (2006, \'MySQL server has gone 
> away\')",)'
>
> Following is another traceback
>
>  File "/home/sys_user/repo/my_app/app/tasks/reminders.py", line 63, in run
>     config = Reminder.query.filter_by(id=reminder_id).first()
>   File 
> "/home/sys_user/Envs/flask/local/lib/python2.7/site-packages/sqlalchemy/orm/query.py",
>  line 2341, in first
>     ret = list(self[0:1])
>   File 
> "/home/sys_user/Envs/flask/local/lib/python2.7/site-packages/sqlalchemy/orm/query.py",
>  line 2208, in __getitem__
>     return list(res)
>   File 
> "/home/sys_user/Envs/flask/local/lib/python2.7/site-packages/sqlalchemy/orm/query.py",
>  line 2412, in __iter__
>     return self._execute_and_instances(context)
>   File 
> "/home/sys_user/Envs/flask/local/lib/python2.7/site-packages/sqlalchemy/orm/query.py",
>  line 2427, in _execute_and_instances
>     result = conn.execute(querycontext.statement, self._params)
>   File 
> "/home/sys_user/Envs/flask/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py",
>  line 729, in execute
>     return meth(self, multiparams, params)
>   File 
> "/home/sys_user/Envs/flask/local/lib/python2.7/site-packages/sqlalchemy/sql/elements.py",
>  line 321, in _execute_on_connection
>     return connection._execute_clauseelement(self, multiparams, params)
>   File 
> "/home/sys_user/Envs/flask/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py",
>  line 826, in _execute_clauseelement
>     compiled_sql, distilled_params
>   File 
> "/home/sys_user/Envs/flask/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py",
>  line 958, in _execute_context
>     context)
>   File 
> "/home/sys_user/Envs/flask/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py",
>  line 1160, in _handle_dbapi_exception
>     exc_info
>   File 
> "/home/sys_user/Envs/flask/local/lib/python2.7/site-packages/sqlalchemy/util/compat.py",
>  line 199, in raise_from_cause
>     reraise(type(exception), exception, tb=exc_tb)
>   File 
> "/home/sys_user/Envs/flask/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py",
>  line 951, in _execute_context
>     context)
>   File 
> "/home/sys_user/Envs/flask/local/lib/python2.7/site-packages/sqlalchemy/engine/default.py",
>  line 436, in do_execute
>     cursor.execute(statement, parameters)
>   File 
> "/home/sys_user/Envs/flask/local/lib/python2.7/site-packages/MySQLdb/cursors.py",
>  line 205, in execute
>     self.errorhandler(self, exc, value)
>   File 
> "/home/sys_user/Envs/flask/local/lib/python2.7/site-packages/MySQLdb/connections.py",
>  line 36, in defaulterrorhandler
>     raise errorclass, errorvalue
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to