title: Trigger Cloud Function based on Firestore changes (Python)
body: <p>Whenever there is change(created, modified, removed) in any document for a particular collection in Firestore, i want to capture the delta and publish it to pubsub.</p>
  
  <p>I am trying all this in python.</p>
  
  <p>Code Snippet :</p>
  
  <pre><code>ref = db.collection('country').document(city_id)
  ref.update(body_json)
  ref.on_snapshot(callback)
  
  def callback(col_snapshot, changes, read_time):
      for change in changes:
          if change.type.name == 'ADDED':
              print('ADDED')
          elif change.type.name == 'MODIFIED':
              print('MODIFIED')
          elif change.type.name == 'REMOVED':
              print('REMOVED')
      print('end of callback')
  </code></pre>
  
  <p>Now, when i make changes in the  firestore document, like :</p>
  
  <ol>
  <li>Add new document - I get the print as <strong>ADDED</strong> and <strong>end of callback</strong>.</li>
  <li>Modify existing document - I mostly get the print as <strong>ADDED</strong> but sometimes or rather once in 10 runs i get both <strong>ADDED</strong> and <strong>MODIFIED</strong>. followed by <strong>end of callback</strong>.</li>
  <li>Remove any document - <strong>end of callback</strong>. Sometimes(rarely) both <strong>REMOVED</strong> and <strong>end of callback</strong>.</li>
  </ol>
  
  <p>I am unable to understand this behaviour and don't know how to deal  with this unable print executions.</p>