12
12
# limitations under the License.
13
13
14
14
import datetime
15
+ import threading
15
16
from time import sleep
16
17
17
18
from google .cloud import firestore
@@ -660,10 +661,14 @@ def listen_document():
660
661
db = firestore .Client ()
661
662
# [START listen_document]
662
663
664
+ # Create an Event for notifying main thread.
665
+ callback_done = threading .Event ()
666
+
663
667
# Create a callback on_snapshot function to capture changes
664
668
def on_snapshot (doc_snapshot , changes , read_time ):
665
669
for doc in doc_snapshot :
666
670
print (u'Received document snapshot: {}' .format (doc .id ))
671
+ callback_done .set ()
667
672
668
673
doc_ref = db .collection (u'cities' ).document (u'SF' )
669
674
@@ -680,7 +685,8 @@ def on_snapshot(doc_snapshot, changes, read_time):
680
685
u'population' : 860000
681
686
}
682
687
doc_ref .set (data )
683
- sleep (3 )
688
+ # Wait for the callback.
689
+ callback_done .wait (timeout = 60 )
684
690
# [START detach_listener]
685
691
# Terminate watch on a document
686
692
doc_watch .unsubscribe ()
@@ -691,12 +697,16 @@ def listen_multiple():
691
697
db = firestore .Client ()
692
698
# [START listen_multiple]
693
699
700
+ # Create an Event for notifying main thread.
701
+ callback_done = threading .Event ()
702
+
694
703
# Create a callback on_snapshot function to capture changes
695
704
def on_snapshot (col_snapshot , changes , read_time ):
696
705
print (u'Callback received query snapshot.' )
697
706
print (u'Current cities in California:' )
698
707
for doc in col_snapshot :
699
708
print (u'{}' .format (doc .id ))
709
+ callback_done .set ()
700
710
701
711
col_query = db .collection (u'cities' ).where (u'state' , u'==' , u'CA' )
702
712
@@ -713,15 +723,18 @@ def on_snapshot(col_snapshot, changes, read_time):
713
723
u'population' : 860000
714
724
}
715
725
db .collection (u'cities' ).document (u'SF' ).set (data )
716
- sleep ( 1 )
717
-
726
+ # Wait for the callback.
727
+ callback_done . wait ( timeout = 60 )
718
728
query_watch .unsubscribe ()
719
729
720
730
721
731
def listen_for_changes ():
722
732
db = firestore .Client ()
723
733
# [START listen_for_changes]
724
734
735
+ # Create an Event for notifying main thread.
736
+ delete_done = threading .Event ()
737
+
725
738
# Create a callback on_snapshot function to capture changes
726
739
def on_snapshot (col_snapshot , changes , read_time ):
727
740
print (u'Callback received query snapshot.' )
@@ -733,6 +746,7 @@ def on_snapshot(col_snapshot, changes, read_time):
733
746
print (u'Modified city: {}' .format (change .document .id ))
734
747
elif change .type .name == 'REMOVED' :
735
748
print (u'Removed city: {}' .format (change .document .id ))
749
+ delete_done .set ()
736
750
737
751
col_query = db .collection (u'cities' ).where (u'state' , u'==' , u'CA' )
738
752
@@ -763,7 +777,9 @@ def on_snapshot(col_snapshot, changes, read_time):
763
777
764
778
# Delete document
765
779
mtv_document .delete ()
766
- sleep (1 )
780
+
781
+ # Wait for the callback captures the deletion.
782
+ delete_done .wait (timeout = 60 )
767
783
query_watch .unsubscribe ()
768
784
769
785
0 commit comments