Search code examples
pythonpython-3.xpython-itertoolspraw

Is there a way to combine posts/comment streams?


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).


Solution

  • 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/