OpenQuizz
Une application de gestion des contenus pédagogiques
ChangeStream Class Reference
Inheritance diagram for ChangeStream:
Collaboration diagram for ChangeStream:

Public Member Functions

def __init__ (self, target, pipeline, full_document, resume_after, max_await_time_ms, batch_size, collation, start_at_operation_time, session, start_after)
 
def close (self)
 
def __iter__ (self)
 
def resume_token (self)
 
def next (self)
 
def alive (self)
 
def try_next (self)
 
def __enter__ (self)
 
def __exit__ (self, exc_type, exc_val, exc_tb)
 

Detailed Description

The internal abstract base class for change stream cursors.

Should not be called directly by application developers. Use 
:meth:`pymongo.collection.Collection.watch`,
:meth:`pymongo.database.Database.watch`, or
:meth:`pymongo.mongo_client.MongoClient.watch` instead.

.. versionadded:: 3.6
.. mongodoc:: changeStreams

Constructor & Destructor Documentation

◆ __init__()

def __init__ (   self,
  target,
  pipeline,
  full_document,
  resume_after,
  max_await_time_ms,
  batch_size,
  collation,
  start_at_operation_time,
  session,
  start_after 
)

Member Function Documentation

◆ __enter__()

def __enter__ (   self)

◆ __exit__()

def __exit__ (   self,
  exc_type,
  exc_val,
  exc_tb 
)

◆ __iter__()

def __iter__ (   self)

◆ alive()

def alive (   self)
Does this cursor have the potential to return more data?

.. note:: Even if :attr:`alive` is ``True``, :meth:`next` can raise
    :exc:`StopIteration` and :meth:`try_next` can return ``None``.

.. versionadded:: 3.8

◆ close()

def close (   self)
Close this ChangeStream.

◆ next()

def next (   self)
Advance the cursor.

This method blocks until the next change document is returned or an
unrecoverable error is raised. This method is used when iterating over
all changes in the cursor. For example::

    try:
resume_token = None
pipeline = [{'$match': {'operationType': 'insert'}}]
with db.collection.watch(pipeline) as stream:
    for insert_change in stream:
        print(insert_change)
        resume_token = stream.resume_token
    except pymongo.errors.PyMongoError:
# The ChangeStream encountered an unrecoverable error or the
# resume attempt failed to recreate the cursor.
if resume_token is None:
    # There is no usable resume token because there was a
    # failure during ChangeStream initialization.
    logging.error('...')
else:
    # Use the interrupted ChangeStream's resume token to create
    # a new ChangeStream. The new stream will continue from the
    # last seen insert change without missing any events.
    with db.collection.watch(
            pipeline, resume_after=resume_token) as stream:
        for insert_change in stream:
            print(insert_change)

Raises :exc:`StopIteration` if this ChangeStream is closed.

◆ resume_token()

def resume_token (   self)
The cached resume token that will be used to resume after the most
recently returned change.

.. versionadded:: 3.9

◆ try_next()

def try_next (   self)
Advance the cursor without blocking indefinitely.

This method returns the next change document without waiting
indefinitely for the next change. For example::

    with db.collection.watch() as stream:
while stream.alive:
    change = stream.try_next()
    # Note that the ChangeStream's resume token may be updated
    # even when no changes are returned.
    print("Current resume token: %r" % (stream.resume_token,))
    if change is not None:
        print("Change document: %r" % (change,))
        continue
    # We end up here when there are no recent changes.
    # Sleep for a while before trying again to avoid flooding
    # the server with getMore requests when no changes are
    # available.
    time.sleep(10)

If no change document is cached locally then this method runs a single
getMore command. If the getMore yields any documents, the next
document is returned, otherwise, if the getMore returns no documents
(because there have been no changes) then ``None`` is returned.

:Returns:
  The next change document or ``None`` when no document is available
  after running a single getMore or when the cursor is closed.

.. versionadded:: 3.8

The documentation for this class was generated from the following file: