混合sqlalchemy和flask-sqlalchemy会话

时间:2015-09-04 03:20:21

标签: python session flask-sqlalchemy

我遇到的问题是将sqlalchemy会话与flask-sqlalchemy会话混合在一​​起。所以对于上下文,我在后台运行一个烧瓶安抚的应用程序,我有一个主方法的文件(runnable.py),该方法应该定期运行平均4周。

此文件使用自己的会话(纯sqlalchemy会话)并连接到此应用程序(使用flask-sqlalchemy会话)。

几个小时后(有时是1天),我收到此错误

Traceback (most recent call last):
  File "/home/ubuntu/cron/runnable.py", line 781, in <module>
    main()
  File "/home/ubuntu/cron/runnable.py", line 677, in main
    db_process_attendee(attendee, eid, event_id)
  File "/home/ubuntu/cron/runnable.py", line 78, in db_process_attendee
    update_attendee_information(attendee, attributes, eid, event_id)
  File "/home/ubuntu/cron/runnable.py", line 117, in update_attendee_information
    update_attendee_profile(attendee, id_attendee_login, eid, event_id)
  File "/home/ubuntu/cron/runnable.py", line 291, in update_attendee_profile
    generate_company_tags_lower.generate_company_tags(id_attendee_login)
  File "/home/ubuntu/cron/app/util/generate_company_tags_lower.py", line 28, in generate_company_tags
    filter(a_p.id_attendee_login == id_attendee_login).\
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/query.py", line 2426, in scalar
    ret = self.one()
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/query.py", line 2395, in one
    ret = list(self)
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/query.py", line 2438, in __iter__
    return self._execute_and_instances(context)
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/query.py", line 2453, in _execute_and_instances
    result = conn.execute(querycontext.statement, self._params)
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 729, in execute
    return meth(self, multiparams, params)
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/elements.py", line 322, in _execute_on_connection
    return connection._execute_clauseelement(self, multiparams, params)
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 826, in _execute_clauseelement
    compiled_sql, distilled_params
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 958, in _execute_context
    context)
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 1159, in _handle_dbapi_exception
    exc_info
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/util/compat.py", line 199, in raise_from_cause
    reraise(type(exception), exception, tb=exc_tb)
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 951, in _execute_context
    context)
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/default.py", line 436, in do_execute
    cursor.execute(statement, parameters)
  File "/usr/local/lib/python2.7/dist-packages/pymysql/cursors.py", line 134, in execute
    result = self._query(query)
  File "/usr/local/lib/python2.7/dist-packages/pymysql/cursors.py", line 282, in _query
    conn.query(q)
  File "/usr/local/lib/python2.7/dist-packages/pymysql/connections.py", line 767, in query
    self._execute_command(COMMAND.COM_QUERY, sql)
  File "/usr/local/lib/python2.7/dist-packages/pymysql/connections.py", line 957, in _execute_command
    self._write_bytes(prelude + sql[:chunk_size-1])
  File "/usr/local/lib/python2.7/dist-packages/pymysql/connections.py", line 916, in _write_bytes
    raise err.OperationalError(2006, "MySQL server has gone away (%r)" % (e,))
sqlalchemy.exc.OperationalError: (OperationalError) (2006, "MySQL server has gone away (error(110, 'Connection timed out'))") 'SELECT a_p.id_attendee_profile AS a_p_id_attendee_profile, a_p.id_attendee_login AS a_p_id_attendee_login \nFROM a_p \nWHERE a_p.id_attendee_login = %s' (128368,) 

我尝试了以下建议,但它们不起作用:

•在我的应用配置中,将“pymysql”添加到SQLALCHEMY_DATABASE_URI中 •在编写sql select查询之前输入generate_company_tags(id_attendee_login)时执行db.session.commit()。我以为会冲洗会议。

顺便说一句,sqlalchemy池循环值设置为小于AWS RDS中的最大值。

关于我如何解决这个问题的任何建议?

0 个答案:

没有答案