我发现使用sqbybject的selectby方法存在泄漏内存。
例如,当我执行此代码时:
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
感谢。
答案 0 :(得分:0)
例如,如果您使用配置文件运行测试 python -m cProfile -s累积 您可以检查问题是否在psycopg2的光标中。
解决方案是 https://sourceforge.net/tracker/?func=detail&atid=540673&aid=3366899&group_id=74338