【发布时间】:2019-06-09 01:13:48
【问题描述】:
我的扭曲服务器连接四个客户端,为每个客户端初始化一次 Sqlalchemy 引擎。系统运行一段时间后,会报如下错误:
[Failure instance: Traceback: <class'sqlalchemy. exc. ResourceClosedError'>: This result object does not return rows. It has been closed automatically.
/usr/lib/python 2.7/threading.py:801:u bootstrap_inner
3/usr/lib/python 2.7/threading.py:754:run
4/home/sites/data_collecting_server/venv/local/lib/python 2.7/site-packages/twisted/_threads/_threadworker.py:46:work
5/home/sites/data_collecting_server/venv/local/lib/python 2.7/site-packages/twisted/_threads/_team.py:190:doWork
6 - < exception caught here >
7/home/sites/data_collecting_server/venv/local/lib/python 2.7/site-packages/twisted/python/threadpool.py:250:inContext
8/home/sites/data_collecting_server/venv/local/lib/python 2.7/site-packages/twisted/python/threadpool.py:266: <lambda>
9/home/sites/data_collecting_server/venv/local/lib/python 2.7/site-packages/twisted/python/context.py:122:call WithContext
10/home/sites/data_collecting_server/venv/local/lib/python 2.7/site-packages/twisted/python/context.py:85:call WithContext
11. / data_server.py:231: check_update_mysqldb
12. / DRV / mysqldb_driver. py: 61: search_device_by_mac
13/home/sites/data_collecting_server/venv/local/lib/python 2.7/site-packages/sqlalchemy/orm/query.py:2895:first:
14/home/sites/data_collecting_server/venv/local/lib/python 2.7/site-packages/sqlalchemy/orm/query.py:2687:u getitem_u
15/home/sites/data_collecting_server/venv/local/lib/python 2.7/site-packages/sqlalchemy/orm/loading.py:98:instances
16/home/sites/data_collecting_server/venv/local/lib/python 2.7/site-packages/sqlalchemy/util/compat.py:265:raise_from_cause
17/home/sites/data_collecting_server/venv/local/lib/python 2.7/site-packages/sqlalchemy/orm/loading.py:61:instances
18/home/sites/data_collecting_server/venv/local/lib/python 2.7/site-packages/sqlalchemy/orm/query.py:3842:row_processor
19/home/sites/data_collecting_server/venv/local/lib/python 2.7/site-packages/sqlalchemy/orm/loading.py:361:_instance_processor
20/home/sites/data_collecting_server/venv/local/lib/python 2.7/site-packages/sqlalchemy/engine/result.py:654:_getter
21/home/sites/data_collecting_server/venv/local/lib/python 2.7/site-packages/sqlalchemy/engine/result.py:1088:_non_result
22]
【问题讨论】:
-
你见过this answer吗? sqlalchemy 作者解释说,您遇到的确切问题与跨线程共享资源有关。你确定不是吗?另请查看 [this](docs.sqlalchemy.org/en/latest/faq/…),这是解决您的特定问题的文档中的一个部分。
-
非常感谢您的回答。我已经看到了你提到的两点。所以我只是使用了 scoped_session 机制。我目前的做法是,每当有客户端连接时,我都会初始化一个Sqlalchemy连接和引擎,然后在当前连接的每个线程函数开头调用set_session(),生成单独的会话进行操作。在线程函数结束时,我调用 close_session() 来删除会话。当前客户端对应的所有线程共享连接和引擎,但会话是独立的。
-
我不知道我为什么这样做,但我仍然收到上面的错误。我的做法有什么问题?
-
但这不是:'与当前客户端对应的所有线程共享连接和引擎',违反了 Mike Bayer 在我上面链接的答案中给出的建议吗?
-
抱歉,可能是我描述的问题。线程共享引擎,会话连接使用scoped_session,每个线程都有单独的会话连接。
标签: python-2.7 sqlalchemy twisted