Am I right that each line of this code makes only one request?
sm = reddit.submission(url="...")
sm.comment_sort = 'top'
sm.comments.replace_more(1)
comments = sm.comments.list()
I know that Reddit api offers 600 requests in each 600 second time period. I need this info to use api more effectively.
This snippet makes two requests. First, when you access sm.comments
on line 3, that loads the comments of the submission from the Reddit API. Then, on that same line, calling replace_more(1)
will replace exactly one MoreComments
object, which makes an additional request.
To figure out how many network requests are being made by some PRAW code, take a look at the section from the documentation on logging. Add this snippet from that page at the top of your script:
import logging
handler = logging.StreamHandler()
handler.setLevel(logging.DEBUG)
logger = logging.getLogger('prawcore')
logger.setLevel(logging.DEBUG)
logger.addHandler(handler)
Then, when you run your script, you'll see debugging output that describes each request made. For your snippet, my output looked something like this:
Fetching: GET https://oauth.reddit.com/comments/fgi5bd/
Data: None
Params: {'limit': 2048, 'sort': 'top', 'raw_json': 1}
Response: 200 (116926 bytes)
Fetching: POST https://oauth.reddit.com/api/morechildren/
Data: [('api_type', 'json'), ('children', 'fk5u680,fk5tgxt,<--snip-->,fk5u67w,fk5ug3f'), ('link_id', 't3_fgi5bd'), ('sort', 'top')]
Params: {'raw_json': 1}
Sleeping: 0.21 seconds prior to call
Response: 200 (32753 bytes)
Each line that starts with "Fetching" is another network request, and the subsequent lines describe the request further.
You say
I know that Reddit api offers 600 requests in each 600 second time period. I need this info to use api more effectively.
While I can't know exactly what you mean by "use api more effectively," if your concern is that you'll go over the rate-limit, you don't have to worry about that at all. One of the key features of PRAW is that it handles the rate limit for you, ensuring that you make requests as frequently as allowed without violating the rate-limit.
If I have 600 requests in each 600 second time period with the code
reddit = praw.Reddit(...) sm = reddit.submission(url="...") sm.comment_sort = 'top' sm.comments.replace_more(597) comments = sm.comments.list()
. Am I right thatlimit=597
fully utilizes one time period without the need in second?Since your existing code makes 2 requests, if you called
.replace_more(599)
, you'd make 600 requests total (on a thread with sufficiently many comments). However, I believe PRAW will attempt to space the 600 requests out as evenly as it can over 600 seconds. I still don't understand exactly what you're trying to do with the rate limit.Ok, thank you, jarhill0!