Yield new items from ``function`` as they become available. :param function: A callable that returns a :class:`.ListingGenerator`, e.g., :meth:`.Subreddit.comments` or :meth:`.Subreddit.new`. :param attribute_name: The field to use as an ID (default: ``"fullname"``). :param excl
(
function: Callable,
*,
attribute_name: str = "fullname",
continue_after_id: str | None = None,
exception_handler: Callable[[Exception], None] | None = None,
exclude_before: bool = False,
pause_after: int | None = None,
skip_existing: bool = False,
**function_kwargs: Any,
)
| 91 | |
| 92 | |
| 93 | def stream_generator( |
| 94 | function: Callable, |
| 95 | *, |
| 96 | attribute_name: str = "fullname", |
| 97 | continue_after_id: str | None = None, |
| 98 | exception_handler: Callable[[Exception], None] | None = None, |
| 99 | exclude_before: bool = False, |
| 100 | pause_after: int | None = None, |
| 101 | skip_existing: bool = False, |
| 102 | **function_kwargs: Any, |
| 103 | ) -> Iterator[Any]: |
| 104 | """Yield new items from ``function`` as they become available. |
| 105 | |
| 106 | :param function: A callable that returns a :class:`.ListingGenerator`, e.g., |
| 107 | :meth:`.Subreddit.comments` or :meth:`.Subreddit.new`. |
| 108 | :param attribute_name: The field to use as an ID (default: ``"fullname"``). |
| 109 | :param exclude_before: When ``True`` does not pass ``params`` to ``function`` |
| 110 | (default: ``False``). |
| 111 | :param exception_handler: A callable that is invoked with the exception raised while |
| 112 | fetching items, instead of letting it propagate and terminate the stream. After |
| 113 | the handler returns, the stream waits (using the same exponential backoff |
| 114 | applied to empty responses) and then resumes. To stop the stream, re-raise the |
| 115 | exception (or raise a new one) from within the handler. When ``None``, |
| 116 | exceptions propagate and terminate the stream as before (default: ``None``). |
| 117 | :param pause_after: An integer representing the number of requests that result in no |
| 118 | new items before this function yields ``None``, effectively introducing a pause |
| 119 | into the stream. A negative value yields ``None`` after items from a single |
| 120 | response have been yielded, regardless of number of new items obtained in that |
| 121 | response. A value of ``0`` yields ``None`` after every response resulting in no |
| 122 | new items, and a value of ``None`` never introduces a pause (default: ``None``). |
| 123 | :param skip_existing: When ``True``, this does not yield any results from the first |
| 124 | request thereby skipping any items that existed in the stream prior to starting |
| 125 | the stream (default: ``False``). |
| 126 | :param continue_after_id: The initial item ID value to use for ``before`` in |
| 127 | ``params``. The stream will continue from the item following this one (default: |
| 128 | ``None``). |
| 129 | |
| 130 | Additional keyword arguments will be passed to ``function``. |
| 131 | |
| 132 | .. note:: |
| 133 | |
| 134 | This function internally uses an exponential delay with jitter between |
| 135 | subsequent responses that contain no new results, up to a maximum delay of just |
| 136 | over 16 seconds. In practice, that means that the time before pause for |
| 137 | ``pause_after=N+1`` is approximately twice the time before pause for |
| 138 | ``pause_after=N``. |
| 139 | |
| 140 | For example, to create a stream of comment replies, try: |
| 141 | |
| 142 | .. code-block:: python |
| 143 | |
| 144 | reply_function = reddit.inbox.comment_replies |
| 145 | for reply in praw.models.util.stream_generator(reply_function): |
| 146 | print(reply) |
| 147 | |
| 148 | To pause a comment stream after six responses with no new comments, try: |
| 149 | |
| 150 | .. code-block:: python |
searching dependent graphs…