sqlobject:使用 selectby 方法泄漏内存
我发现使用 selectby 方法和 sqlobject 存在内存泄漏。
例如,当我执行此代码时:
connection = connectionForURI('postgresql://test:test@localhost/test_sql_object')
tran = connection.transaction()
sqlhub.threadConnection = tran
class Person(SQLObject):
firstName = StringCol()
middleInitial = StringCol(length=1, default=None)
lastName = StringCol()
for iteration in range(20):
start = datetime.now()
for key in range(1000):
person = Person.selectBy(firstName='name%s%s' % (iteration,key))
if person:
select = person[0]
end = datetime.now()
print "TIME ",iteration,':' , end - start
结果是
TIME 0 : 0:00:03.328198
TIME 1 : 0:00:03.382905
TIME 2 : 0:00:03.690991
TIME 3 : 0:00:04.436301
TIME 4 : 0:00:05.021656
TIME 5 : 0:00:05.393993
TIME 6 : 0:00:05.791572
TIME 7 : 0:00:06.151833
TIME 8 : 0:00:06.517327
TIME 9 : 0:00:06.779239
TIME 10 : 0:00:06.961454
TIME 11 : 0:00:06.872361
TIME 12 : 0:00:07.114973
TIME 13 : 0:00:07.473208
TIME 14 : 0:00:07.737618
TIME 15 : 0:00:07.951056
TIME 16 : 0:00:08.199360
TIME 17 : 0:00:08.600283
TIME 18 : 0:00:08.802639
TIME 19 : 0:00:09.131514s
我测试使用connection.clear()、expire等,但得到相同的结果。 例如,如果我更改 Person 类,
class Person(SQLObject):
class sqlmeta:
cacheValues = False
firstName = StringCol()
middleInitial = StringCol(length=1, default=None)
lastName = StringCol()
结果是
TIME 0 : 0:00:03.298583
TIME 1 : 0:00:03.465153
TIME 2 : 0:00:03.955067
TIME 3 : 0:00:04.594931
TIME 4 : 0:00:05.062099
TIME 5 : 0:00:05.172968
TIME 6 : 0:00:06.011087
TIME 7 : 0:00:06.106087
TIME 8 : 0:00:06.475573
TIME 9 : 0:00:06.836605
TIME 10 : 0:00:06.963226
TIME 11 : 0:00:06.889263
TIME 12 : 0:00:07.219188
TIME 13 : 0:00:07.417601
TIME 14 : 0:00:07.737096
TIME 15 : 0:00:08.128259
TIME 16 : 0:00:08.217653
TIME 17 : 0:00:08.612468
TIME 18 : 0:00:08.800511
TIME 19 : 0:00:08.979550
Thanks。
I had discovered that using selectby method with sqlobject there are leak memory.
For example, when I execute this code:
connection = connectionForURI('postgresql://test:test@localhost/test_sql_object')
tran = connection.transaction()
sqlhub.threadConnection = tran
class Person(SQLObject):
firstName = StringCol()
middleInitial = StringCol(length=1, default=None)
lastName = StringCol()
for iteration in range(20):
start = datetime.now()
for key in range(1000):
person = Person.selectBy(firstName='name%s%s' % (iteration,key))
if person:
select = person[0]
end = datetime.now()
print "TIME ",iteration,':' , end - start
and the result is
TIME 0 : 0:00:03.328198
TIME 1 : 0:00:03.382905
TIME 2 : 0:00:03.690991
TIME 3 : 0:00:04.436301
TIME 4 : 0:00:05.021656
TIME 5 : 0:00:05.393993
TIME 6 : 0:00:05.791572
TIME 7 : 0:00:06.151833
TIME 8 : 0:00:06.517327
TIME 9 : 0:00:06.779239
TIME 10 : 0:00:06.961454
TIME 11 : 0:00:06.872361
TIME 12 : 0:00:07.114973
TIME 13 : 0:00:07.473208
TIME 14 : 0:00:07.737618
TIME 15 : 0:00:07.951056
TIME 16 : 0:00:08.199360
TIME 17 : 0:00:08.600283
TIME 18 : 0:00:08.802639
TIME 19 : 0:00:09.131514s
I test use connection.clear(), expire, etc but I get the same result.
For example, if I change Person class
class Person(SQLObject):
class sqlmeta:
cacheValues = False
firstName = StringCol()
middleInitial = StringCol(length=1, default=None)
lastName = StringCol()
The result is
TIME 0 : 0:00:03.298583
TIME 1 : 0:00:03.465153
TIME 2 : 0:00:03.955067
TIME 3 : 0:00:04.594931
TIME 4 : 0:00:05.062099
TIME 5 : 0:00:05.172968
TIME 6 : 0:00:06.011087
TIME 7 : 0:00:06.106087
TIME 8 : 0:00:06.475573
TIME 9 : 0:00:06.836605
TIME 10 : 0:00:06.963226
TIME 11 : 0:00:06.889263
TIME 12 : 0:00:07.219188
TIME 13 : 0:00:07.417601
TIME 14 : 0:00:07.737096
TIME 15 : 0:00:08.128259
TIME 16 : 0:00:08.217653
TIME 17 : 0:00:08.612468
TIME 18 : 0:00:08.800511
TIME 19 : 0:00:08.979550
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
例如,如果您使用配置文件运行测试
python -m cProfile -s 累积
您可以检查问题是否出在psycopg2的光标上。
解决方案是在
https://sourceforge.net/tracker/?func=detail&atid=540673&aid=3366899&group_id=74338
If you run the test with profile, for example
python -m cProfile -s cumulative
You can check that the problem is in the cursor of psycopg2.
The solution is in
https://sourceforge.net/tracker/?func=detail&atid=540673&aid=3366899&group_id=74338