我有一个芹菜项目连接到MySQL数据库。其中一个表的定义如下:
class MyQueues(Base):
__tablename__ = 'accepted_queues'
id = sa.Column(sa.Integer, primary_key=True)
customer = sa.Column(sa.String(length=50), nullable=False)
accepted = sa.Column(sa.Boolean, default=True, nullable=False)
denied = sa.Column(sa.Boolean, default=True, nullable=False)
而且,在我的设置中
THREADS = 4
我被困在code.py
的函数中:
def load_accepted_queues(session, mode=None):
#make query
pool = session.query(MyQueues.customer, MyQueues.accepted, MyQueues.denied)
#filter conditions
if (mode == 'XXX'):
pool = pool.filter_by(accepted=1)
elif (mode == 'YYY'):
pool = pool.filter_by(denied=1)
elif (mode is None):
pool = pool.filter(\
sa.or_(MyQueues.accepted == 1, MyQueues.denied == 1)
)
#generate a dictionary with data
for i in pool: #<---------- line 90 in the error
l.update({i.customer: {'customer': i.customer, 'accepted': i.accepted, 'denied': i.denied}})
运行此命令时,出现错误:
[20130626 115343] Traceback (most recent call last):
File "/home/me/code/processing/helpers.py", line 129, in wrapper
ret_value = func(session, *args, **kwargs)
File "/home/me/code/processing/test.py", line 90, in load_accepted_queues
for i in pool: #generate a dictionary with data
File "/home/me/envs/me/local/lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 2341, in instances
fetch = cursor.fetchall()
File "/home/me/envs/me/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 3205, in fetchall
l = self.process_rows(self._fetchall_impl())
File "/home/me/envs/me/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 3174, in _fetchall_impl
self._non_result()
File "/home/me/envs/me/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 3179, in _non_result
"This result object does not return rows. "
ResourceClosedError: This result object does not return rows. It has been closed automatically
所以主要是部分
ResourceClosedError: This result object does not return rows. It has been closed automatically
有时也会出现这样的错误:
DBAPIError: (Error) (, AssertionError('Result length not requested length:\nExpected=1. Actual=0. Position: 21. Data Length: 21',)) 'SELECT accepted_queues.customer AS accepted_queues_customer, accepted_queues.accepted AS accepted_queues_accepted, accepted_queues.denied AS accepted_queues_denied \nFROM accepted_queues \nWHERE accepted_queues.accepted = %s OR accepted_queues.denied = %s' (1, 1)
我不能像处理大量数据时通常发生的那样正确地再现错误。我试图将THREADS = 4
更改为1
,错误消失了。无论如何,这不是一个解决方案,因为我需要将线程数保持在4
。
另外,我对使用
for i in pool: #<---------- line 90 in the error
或者
for i in pool.all(): #<---------- line 90 in the error
找不到合适的解释。
总而言之:有什么建议可以跳过这些困难吗?
是的。您绝对不能同时在多个线程中使用会话(或与该会话相关联的任何对象)或连接,特别是对于DBAPI连接非常不安全的MySQL Python*。您必须对应用程序进行组织,使每个线程处理自己的专用MySQL-Python连接(因此与该会话相关联的SQLAlchemy连接/会话/对象),而不会泄漏到任何其他线程。
相关问题 更多 >
编程相关推荐