Python 脚本被杀死
Python script getting Killed
环境
烧瓶 0.10.1
SQL炼金术1.0.10
Python3.4.3
使用单元测试
我创建了两个单独的测试,其目标是通过 700k 记录查看数据库并进行一些字符串查找。当一次执行一个测试时,它工作正常,但是当整个脚本执行时:
python name_of_script.py
它在随机位置以 "KILLED" 退出。
两个测试的主要代码是这样的:
def test_redundant_categories_exist(self):
self.assertTrue(self.get_redundant_categories() > 0, 'There are 0 redundant categories to remove. Cannot test removing them if there are none to remove.')
def get_redundant_categories(self):
total = 0
with get_db_session_scope(db.session) as db_session:
records = db_session.query(Category)
for row in records:
if len(row.c) > 1:
c = row.c
#TODO: threads, each thread handles a bulk of rows
redundant_categories = [cat_x.id
for cat_x in c
for cat_y in c
if cat_x != cat_y and re.search(r'(^|/)' + cat_x.path + r'($|/)', cat_y.path)
]
total += len(redundant_categories)
records = None
db_session.close()
return total
另一个测试调用位于 manager.py
文件中的函数,它执行类似的操作,但在数据库中添加了批量删除。
def test_remove_redundant_mappings(self):
import os
os.system( "python ../../manager.py remove_redundant_mappings" )
self.assertEqual(self.get_redundant_categories(), 0, "There are redundant categories left after running manager.py remove_redundant_mappings()")
是否可以在测试之间将数据保存在内存中?我不太明白单独执行测试是如何工作的,但是当 运行 背靠背时,进程以 Killed 结束。
有什么想法吗?
编辑(我试过无济于事):
- 从
manager.py
导入函数并在没有 os.system(..)
的情况下调用它
import gc
和 运行 gc.collect()
在 get_redundant_categories()
之后和调用 remove_redundant_mappings()
之后
在搜索高点和低点时,我偶然发现了这个 Whosebug question/answer
中的以下评论
What is happening, I think, is that people are instantiating sessions and not closing them. The objects are then being garbage collected without closing the sessions. Why sqlalchemy sessions don't close themselves when the session object goes out of scope has always and will always be beyond me. @melchoir55
所以我在被测试的方法中添加了以下内容:
db_session.close()
现在单元测试执行时不会被杀死。
环境 烧瓶 0.10.1 SQL炼金术1.0.10 Python3.4.3
使用单元测试
我创建了两个单独的测试,其目标是通过 700k 记录查看数据库并进行一些字符串查找。当一次执行一个测试时,它工作正常,但是当整个脚本执行时:
python name_of_script.py
它在随机位置以 "KILLED" 退出。
两个测试的主要代码是这样的:
def test_redundant_categories_exist(self):
self.assertTrue(self.get_redundant_categories() > 0, 'There are 0 redundant categories to remove. Cannot test removing them if there are none to remove.')
def get_redundant_categories(self):
total = 0
with get_db_session_scope(db.session) as db_session:
records = db_session.query(Category)
for row in records:
if len(row.c) > 1:
c = row.c
#TODO: threads, each thread handles a bulk of rows
redundant_categories = [cat_x.id
for cat_x in c
for cat_y in c
if cat_x != cat_y and re.search(r'(^|/)' + cat_x.path + r'($|/)', cat_y.path)
]
total += len(redundant_categories)
records = None
db_session.close()
return total
另一个测试调用位于 manager.py
文件中的函数,它执行类似的操作,但在数据库中添加了批量删除。
def test_remove_redundant_mappings(self):
import os
os.system( "python ../../manager.py remove_redundant_mappings" )
self.assertEqual(self.get_redundant_categories(), 0, "There are redundant categories left after running manager.py remove_redundant_mappings()")
是否可以在测试之间将数据保存在内存中?我不太明白单独执行测试是如何工作的,但是当 运行 背靠背时,进程以 Killed 结束。
有什么想法吗?
编辑(我试过无济于事):
- 从
manager.py
导入函数并在没有os.system(..)
的情况下调用它
import gc
和 运行gc.collect()
在get_redundant_categories()
之后和调用remove_redundant_mappings()
之后
在搜索高点和低点时,我偶然发现了这个 Whosebug question/answer
中的以下评论What is happening, I think, is that people are instantiating sessions and not closing them. The objects are then being garbage collected without closing the sessions. Why sqlalchemy sessions don't close themselves when the session object goes out of scope has always and will always be beyond me. @melchoir55
所以我在被测试的方法中添加了以下内容:
db_session.close()
现在单元测试执行时不会被杀死。