0

有没有人注意到 praw 的 get_redditor() 方法有点慢?我昨天开始使用它,当我开始注意到速度下降时,我决定做一些测试。我使用以下代码:

import praw
import time

time1 = 0
time2 = 0

print('Initial reddit setup')
time1 = int(round(time.time() * 1000))
user_agent = ('Reddit Checker 0.1 by /u/NinjaXI')
r = praw.Reddit(user_agent)
time2 = int(round(time.time() * 1000))
print(time1)
print(time2)
print(time2 - time1)
print('===================================================================')

print('Get Subreddit Python')
time1 = int(round(time.time() * 1000))
pythonSub = r.get_subreddit('Python')
time2 = int(round(time.time() * 1000))
print(time1)
print(time2)
print(time2 - time1)
print('===================================================================')

print('Get hot 10')
time1 = int(round(time.time() * 1000))
pythonSub.get_hot(limit=10)
time2 = int(round(time.time() * 1000))
print(time1)
print(time2)
print(time2 - time1)
print('===================================================================')

print('Get Redditor NinjaXI')
time1 = int(round(time.time() * 1000))
me = r.get_redditor('NinjaXI')
time2 = int(round(time.time() * 1000))
print(time1)
print(time2)
print(time2 - time1)
print('===================================================================')

print('Get submitted 10')
time1 = int(round(time.time() * 1000))
me.get_submitted(limit=10)
time2 = int(round(time.time() * 1000))
print(time1)
print(time2)
print(time2 - time1)
print('===================================================================')

这输出:

Initial reddit setup
1421319134782
1421319134783
1
===================================================================
Get Subreddit Python
1421319134783
1421319134783
0
===================================================================
Get hot 10
1421319134783
1421319134783
0
===================================================================
Get Redditor NinjaXI
1421319134783
1421319137402
2619
===================================================================
Get submitted 10
1421319137402
1421319137402
0
===================================================================

如您所见,使用 get_redditor() 比 get_subreddit() 需要(在此运行中)多 2.6 秒。平均每次时间在 2 到 2.6 秒之间,但 get_subreddit() 仅高达 1 毫秒。我在做什么吗?这是 praw/reddit api 的已知问题吗?我有什么办法可以加快速度吗?

4

1 回答 1

1

PRAW 的 getter 通常返回惰性对象,这意味着它们仅在您实际尝试使用该对象时从 Reddit 的服务器请求数据。这与您非常快速的函数调用一致——一切都在本地发生。您的get_redditor()电话可能是立即请求数据(尽管我无法说出原因;根据文档,它也应该是懒惰的)。

于 2015-01-15T13:40:35.290 回答