4

In praw, I can create either a subreddit.stream.comments() or subreddit.stream.submissions().

For those unfamiliar, the two above praw functions return comments/posts as they come in.

Is there a way to combine the two? I've tried using Python's built-in function zip as well as itertools's zip_longest but they both only give a result as fast as the posts come in. (Comments are much more frequent).

kpaul
  • 355
  • 6
  • 16

1 Answers1

3

Found the answer:

comment_stream = subreddit.stream.comments(pause_after=-1)
submission_stream = subreddit.stream.submissions(pause_after=-1)
while True:
    for comment in comment_stream:
        if comment is None:
            break
        print(comment.author)
    for submission in submission_stream:
        if submission is None:
            break
        print(submission.title)

The key is the pause_after parameter.

Source: https://www.reddit.com/r/redditdev/comments/7vj6ox/can_i_do_other_things_with_praw_while_reading/dtszfzb/

kpaul
  • 355
  • 6
  • 16
  • Probably it would be a good idea to add some kind of sleep in the main loop when none of the two streams returned new items. – sth Sep 30 '18 at 18:54