gpt4 book ai didi

Linux 上的 Python SQLAlchemy 内存泄漏

转载 作者:太空宇宙 更新时间:2023-11-03 15:21:03 32 4
gpt4 key购买 nike

我编写了一个脚本来遍历一个大型数据库表。 (~150K 行。)为了避免使用太多内存,我使用了这个 windowed_query method .我的脚本是这样的:

query = db.query(Table)

count = 0
for row in windowed_query(query, Table.id, 1000):

points = 0

# +100 points for a logo
if row.logo_id:
points += 100

# +10 points for each image
points += 10 * len(row.images) #images is a SQLAlchemy one-to-many relationship

#...The script continues with much of the same...

row.points = points
db.add(row)

count += 1
if count % 100 == 0:
db.commit()
print count

request.db.commit()

当尝试在 CentOS 服务器上运行它时,它在被内核杀死之前完成了 9000 行,因为它使用了大约 2GB 的内存

在我的 Mac 开发环境中,即使它运行在完全相同版本的 Python (2.7.3)、SQLAlchemy (0.7.8) 和 psycopg2 (2.4.5) 上,它也能正常工作。

使用memory_profiler进行一些简单的调试:在Linux上,查询数据库的每段代码都会少量增加内存,增长从未停止。在 Mac 上,同样的事情发生了,但在增长了 ~4MB 之后就趋于平稳。就好像在 Linux 上什么都没有被垃圾收集一样。 (我什至尝试每 100 行运行一次 gc.collect()。什么也没做。)

有人知道发生了什么事吗?

最佳答案

原来 Pyramid 的 debugtoolbar 被启用了,这就是高内存使用的原因。我禁用了它,脚本运行得非常棒。

关于Linux 上的 Python SQLAlchemy 内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14800018/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com