title: Firebase Cloud Firestore onSnapshot for subcollection on Python
body: <p>【Updated】I'm working on the following python code.
  As its preset, there are only 2 users in Cloud Firestore.</p>
  <pre><code>def on_snapshot(doc_snapshot, changes, read_time):
  
      user_counter = 0
      for doc in doc_snapshot:
          print('user %s' %(user_counter))
          user_counter +=1
  
  
  while_counter=0
  while True:
      print('while %s ----------' %(while_counter))
      while_counter+=1
  
      time.sleep(1) #Sleep for X seconds
  
      #doc_ref = db.collection(u'users').document(doc.id)
      doc_ref = db.collection(u'users')    # Watch the document
      doc_watch = doc_ref.on_snapshot(on_snapshot)
  </code></pre>
  <p>And the terminal showed following output,</p>
  <pre><code>while 0 ----------
  while 1 ----------
  user 0
  user 1
  while 2 ----------
  user 0
  user 1
  while 3 ----------
  user 0
  user 1
  while 4 ----------
  user 0
  user 1
  while 5 ----------
  user 0
  user 1
  user 0user 0
  user 1
  
  user 0
  user 1
  user 1
  user 0
  user 1
  user 0
  user 1
  while 6 ----------
  user 0
  user 1
  while 7 ----------
  user 0
  user 1
  while 8 ----------
  user 0
  user 1
  user 0
  user 1
  user 0
  user 1
  user 0
  user 1
  user 0
  user 1
  user 0
  user 1
  user 0
  user 1
  user 0
  user 1
  user 0
  user 1
  while 9 ----------
  user 0
  user 1
  while 10 ----------
  user 0
  user 1
  </code></pre>
  <p>though the output should be like this in my thought.</p>
  <pre><code>while 0 ----------
  user 0
  user 1
  while 1 ----------
  user 0
  user 1
  while 2 ----------
  user 0
  user 1
  while 3 ----------
  user 0
  user 1
  while 4 ----------
  user 0
  user 1
  while 5 ----------
  user 0
  user 1
  while 6 ----------
  user 0
  user 1
  while 7 ----------
  user 0
  user 1
  while 8 ----------
  user 0
  user 1
  while 9 ----------
  user 0
  user 1
  while 10 ----------
  user 0
  user 1
  </code></pre>
  <p>I've been stuck on this issue for a while.
  So I'd appreciate it, if someone would find and tell me a solution.
  Thank you.</p>
  <hr />
  <p>【Past post of this issue】
  I'm working on a python code below.</p>
  <pre><code>def on_snapshot(doc_snapshot, changes, read_time):
      user_counter = 0
      for doc in doc_snapshot:
          print('user_counter: %s' %(user_counter))
          user_counter +=1
  
          sub_docs = db.collection(u'users').document(doc.id).collection(u'history').where(u'flag', u'==', True).stream()
  
          subdoc_counter = 0
          for sub_doc in sub_docs:
              print('subdoc_counter: %s' %(subdoc_counter))
              subdoc_counter +=1
  
              print('Generating...')
              sub_doc_dict = sub_doc.to_dict()
              usr_txt = sub_doc_dict['inputdata']
  
              #doing something for updating usr_text, here.
  
              db.collection(u'users').document(doc.id).collection(u'history').document(sub_doc.id).set({
                  u'outputdata': usr_txt,
                  u'flag': False
              }, merge=True)
  
              #break #This didn't work I expected.
  
  
  doc_ref = db.collection(u'users')
  while_counter=0
  while True:
      doc_watch = doc_ref.on_snapshot(on_snapshot)
  
      print('while_counter: %s' %(while_counter))
      while_counter+=1
  
      time.sleep(1)
  </code></pre>
  <p>And the output of print() is below,</p>
  <pre><code>#Actual output of print().
  &quot;&quot;&quot;
  while_counter: 0
  user_counter: 0
  while_counter: 1
  user_counter: 0
  user_counter: 1
  user_counter: 1
  while_counter: 2
  user_counter: 0
  user_counter: 1
  .
  .
  .
  
  while_counter: 12
  user_counter: 1
  user_counter: 1
  user_counter: 1
  user_counter: 1
  user_counter: 1
  user_counter: 1
  user_counter: 0
  subdoc_counter: 0
  Generating...
  subdoc_counter: 0
  Generating...
  user_counter: 1
  subdoc_counter: 0
  Generating...
  subdoc_counter: 0
  Generating...
  user_counter: 1
  subdoc_counter: 0
  Generating...
  subdoc_counter: 0while_counter: 13
  
  Generating...
  subdoc_counter: 0
  Generating...
  subdoc_counter: 0
  Generating...
  user_counter: 1
  subdoc_counter: 0
  Generating...
  subdoc_counter: 0
  Generating...
  subdoc_counter: 0
  Generating...subdoc_counter: 0
  Generating...
  
  user_counter: 0
  user_counter: 1
  while_counter: 14
  user_counter: 0
  user_counter: 1
  while_counter: 15
  user_counter: 0
  user_counter: 1
  &quot;&quot;&quot;
  </code></pre>
  <p>though I though it should show like below.</p>
  <pre><code>#Output of my thought.
  &quot;&quot;&quot;
  while_counter: 0
  user_counter: 0
  user_counter: 1
  while_counter: 1
  user_counter: 0
  user_counter: 1
  while_counter: 2
  user_counter: 0
  user_counter: 1
  .
  .
  .
  
  while_counter: 12
  user_counter: 0
  subdoc_counter: 0
  Generating...
  user_counter: 1
  while_counter: 13
  user_counter: 0
  user_counter: 1
  .
  .
  .
  &quot;&quot;&quot;
  </code></pre>
  <p>What I can't tell is why my code run print('Generating...') many times. And how to modify it.
  This Cloud Firestore has subcollection 'history' for each user (Now there are just two users, there).
  In 'history', there are history data like history1, history2... .
  In each history data, there are three values: flag, inputdata and outputdata.
  When to turn on flag(=True), outputdata is generated from inputdata just once (When to start this python code, all of flags have False).
  And there is only one(or zero) history data which has flag=True at the same time in a user's subcollection.</p>
  <p>Did you find any solution to let this code run like '#Output of my thought.'?
  Thank you in advance.</p>