为什么这个 python 脚本会慢慢消耗我的 RAM?

发布于 2024-12-15 00:53:55 字数 785 浏览 0 评论 0原文

这个脚本慢慢地消耗了我的内存。当我运行它时,我可以看到每次循环 Python 的 RAM 使用量都会增加大约 1mb,但我不明白为什么。我已经发现是查询的迭代增加了 RAM,但这就是我能弄清楚的全部。任何帮助都会很棒。

from haystack.pmod import piliPlacement #this is the SA model
from time import sleep
from datetime import datetime
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker


now = datetime.now()


engine = create_engine('mssql://xxxxxxx;uid=user;pwd=xxxxx',echo=False)


Session = sessionmaker(bind=engine)


def syncPlacements(session):
    query = session.query(piliPlacement).filter(piliPlacement.Time > now)
    pili_placements = [p.ID_Placement for p in query.all()] # this is what adds the RAM
    del pili_placements
    print 'loop'


while True:
    session = Session()
    syncPlacements(session)
    sleep(3)

This script slowly eats my RAM. When I run it, I can see the RAM usage of Python creep up by approx 1mb with each loop, but I can't figure out why. I have figured out that it is the iteration of the query that adds the RAM, but that's all I can figure out. Any help would be awesome.

from haystack.pmod import piliPlacement #this is the SA model
from time import sleep
from datetime import datetime
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker


now = datetime.now()


engine = create_engine('mssql://xxxxxxx;uid=user;pwd=xxxxx',echo=False)


Session = sessionmaker(bind=engine)


def syncPlacements(session):
    query = session.query(piliPlacement).filter(piliPlacement.Time > now)
    pili_placements = [p.ID_Placement for p in query.all()] # this is what adds the RAM
    del pili_placements
    print 'loop'


while True:
    session = Session()
    syncPlacements(session)
    sleep(3)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

深海里的那抹蓝 2024-12-22 00:53:55

在将其剥离回来并与 SA IRC 频道上的一个人聊天后,这似乎是 Mac OSX 独有的问题。所以我在Linux上设置了它,但发生了同样的事情。最后,我求助于在 crontab 上运行脚本。现在工作正常。

中号

After stripping it right back, and chatting to a guy on the SA IRC channel, it appeared to be a Mac OSX only problem. So I set it up on Linux but the same thing occured. In the end, I resorted to running the script on a crontab. Works fine now.

M

后来的我们 2024-12-22 00:53:55

您可能想看看 mdb 是否随着工作量的增加而慢慢变大。这就是为什么我假设这种情况会发生。在您的示例中,它是唯一看起来不会被垃圾收集的对象,而且它也恰好是一个对象。因此,随着它的使用越来越多,我肯定会研究它的用途。

我想知道它是否维护已执行的查询列表。

You might want to see if mdb is slowly getting larger as it does more work. That is why I'd assume that this would be occuring. It is the only object that doesn't look like it would be garbage collected in your example, and it also happens to be an object. So, I would definitely look into what it's doing as it is used more and more.

I'd wonder if it's maintaining a list of queries that have executed.

你对谁都笑 2024-12-22 00:53:55

只有 3 个问题

session 从哪里来?

为什么不将 mdb = Connection().haystack 放入 while True 循环中(顺便说一句,session.close() 也是如此)?并将 mdbsession 传递给 syncPlacements

整个代码块被复制,只有两个差异(即计算匹配,保存或删除)?

Just 3 questions

Where does session come from?

Why not put mdb = Connection().haystack into the while True loop (and btw session.close() too)? And pass mdb and session to syncPlacements?

Whole code block is copied just with just two differences (i.e. calculate matches, save or remove)?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文