Firebase RTDB 侦听器不适用于 FastAPI

Firebase RTDB Listener doesn't work with FastAPI

这是我与 Firebase RTDB 监听器相关的代码:

import firebase_admin
from firebase_admin import credentials
from firebase_admin import db


class FirebaseConfig:
    reference = None
    listenerRegistration = None

    def __init__(self):
        cred = credentials.Certificate("path_to_credentials.json")
        firebase_admin.initialize_app(
            cred,
            {'databaseURL': 'database_url'}
        )
        self.reference = db.reference("Users")
        self.listenerRegistration = self.reference.listen(self.userListener)

    def userListener(self, event):
        print("Listener Started.")
        try:
            print(event.data)
            updateData() # A function to update our local database
        except Exception as e:
            raise Exception("Listener Error" + str(e))

    def stopListening(self):
        print("Closing Listener")
        self.listenerRegistration.close()

我现在面临的问题是这个监听器不起作用。打印语句不打印任何内容。我认为这是因为 FastAPI。有什么方法可以让它发挥作用吗?

如果它们不能很好地协同工作,您是否有任何替代解决方案?

我想这可能是因为我为我的 uvicorn 服务器使用了端口 80。我改变了它并且它起作用了。 这是最可能的原因。不能肯定。