title: MongoDB Change Stream: How to keep it alive?
body: <p>I've been trying to implement a change stream that monitors a Mongo collection for new documents.  While simple to setup for catching one change, I don't understand how to keep the process running indefinitely.</p>
  <pre><code>db = pymongo_util.get_collection(&quot;DataDB&quot;,&quot;XYZ_Collection&quot;)
  stream = db.watch(full_document=&quot;updateLookup&quot;):
  document = next(stream)  # it blocks here until a change happens.  Prints the change, and program ends
  print(document)
  </code></pre>
  <p>My goal is to create a 'listener' for the database.   Listen for new documents, and process those new documents.  I'm not sure if asyncio is the way to go, threading, or if its something basic I'm missing.</p>
  <p>It looks like I'm not the only one to ask, but none seem to have an answer:</p>
  <ol>
  <li><p><a href="https://stackoverflow.com/questions/66391256/how-to-actually-use-pymongo-changestreams-with-flask-in-a-non-blocking-way">How to actually use pymongo ChangeStreams with Flask in a non-blocking way?</a></p>
  </li>
  <li><p><a href="https://stackoverflow.com/questions/68980881/watch-mongodb-change-streams-in-python-asynchronous">Watch MongoDB Change Streams in Python asynchronous</a></p>
  </li>
  </ol>