简体   繁体   English

为什么这个python脚本慢慢地嚼我的RAM?

[英]Why is this python script slowly chewing up my RAM?

This script slowly eats my RAM. 这个脚本慢慢地吃了我的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. 当我运行它时,我可以看到Python的RAM使用量每个循环增加大约1mb,但我无法弄清楚原因。 I have figured out that it is the iteration of the query that adds the RAM, but that's all I can figure out. 我已经发现它是查询的迭代添加了RAM,但这是我能想到的。 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)

After stripping it right back, and chatting to a guy on the SA IRC channel, it appeared to be a Mac OSX only problem. 在将其剥离后,与SA IRC频道上的某个人聊天后,它似乎只是Mac OSX的一个问题。 So I set it up on Linux but the same thing occured. 所以我在Linux上设置它但同样的事情发生了。 In the end, I resorted to running the script on a crontab. 最后,我使用了crontab上的脚本。 Works fine now. 现在工作正常。

M 中号

You might want to see if mdb is slowly getting larger as it does more work. 您可能想看看mdb是否正在慢慢变大,因为它会更多工作。 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. 我想知道它是否正在维护已执行的查询列表。

Just 3 questions 只有3个问题

Where does session come from? session来自哪里?

Why not put mdb = Connection().haystack into the while True loop (and btw session.close() too)? 为什么不将mdb = Connection().haystack放入while True循环(和btw session.close() )? And pass mdb and session to syncPlacements ? 并将mdbsession传递给syncPlacements

Whole code block is copied just with just two differences (ie calculate matches, save or remove)? 整个代码块只复制两个差异(即计算匹配,保存或删除)?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM