googleapis / python-firestore

Apache License 2.0
214 stars 74 forks source link

Compatibility with Gevent #45

Closed leodaher closed 2 years ago

leodaher commented 4 years ago

I need to use the realtime snapshot available from Firestore, and in my application, I am trying to use it inside a Gevent Greenlet, but it does not work as expected. Here's a sample script:

import gevent
import time

import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore

key = "/path/to/serviceAccountCredentials.json"

def listen():
    cred = credentials.Certificate(key)
    firebase_admin.initialize_app(cred)
    db = firestore.client()

    def callback(doc_snapshot, changes, read_time):
        print("Received changes")

    db.collection('orders').on_snapshot(callback)

    while True:
        pass

if __name__ == '__main__':
    gevent.spawn(listen)
    while True:
        pass

If I make any changes to the orders collection, nothing is printed in the stdout. Now, if I change the last lines of the script so it doesn't run inside Gevent, it works as expected:

if __name__ == '__main__':
    listen()

These are the environment details in which I am running the script:

jamadden commented 4 years ago

Also reported in https://github.com/gevent/gevent/issues/1615. There are a number of issues with the example provided that need to be worked through first.

HemangChothani commented 4 years ago

@leodaher I am not familiar with 'Gevent' library ,but in your scenario you can use sleep or join method which will help to get output.

code snippet:

if __name__ == '__main__':
    gevent.spawn(listen)
    while True:
        gevent.sleep(1)

OR:

if __name__ == '__main__':
    gev = gevent.spawn(listen)
    gev.join()