I have been using firebase realtime database recently and I am not sure as to why when I create a database reference in python, I can not simply get the data at that location. In the IPython, I can do the following successfully:
from firebase_admin import credentials, auth, initialize_app, db
cred = credentials.Certificate("my_certificate.json")
initialize_app(cred, {'databaseURL':'link_to_my_database'})
ref = db.reference("some_text_here")
print(ref.get())
>>> desired output
But when I put this code in my REST API code (using FastAPI), I get a "None" value. I know firebase for python is a blocking I/O, but I don't understand how that could be affecting my code if everything is working in IPython? I would really appreciate any help in showing me where I need to fix my code. I'm not sure if I should just switch to Node.js because of this blocking I/O stuff with Python
Update (June 28, 2020)
Based on a comment on the question I have added my FastAPI code below to show where I am having issues:
Router.py
import asyncio
from app.controllers.pyController import PyController
async def new_func(some_data: str, udata:dict):
eventLoop = asyncio.get_event_loop()
try:
eventLoop.run_until_complete(PyController().pyController_func(some_data, udata, eventLoop))
finally:
print("maybe we're done?")
pyController.py
import redis
from app.services.runPyService import RunPyService
class PyController:
async def pyController_func(self, some_data, uData, eventLoop):
r = redis.Redis()
dStoreVal = r.hget(some_data, "data")
done = await RunPyService().runPyService_func(dStoreVal, uData, eventLoop)
runPyService.py
from firebase_admin import db
import asyncio
import concurrent
class RunPyService:
executor = concurrent.futures.ThreadPoolExecutor(max_workers=40)
async def runPyService_func(self, dStoreVal, uData, eventLoop):
coroutine = [self.getData(dStoreVal, eventLoop)]
completed, pending = await asyncio.wait(coroutine)
for item in completed:
print(item.result())
async def getData(self, dStoreVal, eventLoop):
ref = db.reference("where-data-is-located")
return await eventLoop.run_in_executor(self.executor, ref.get)
I am passing the eventLoop that I get from the router through the controller to the service where I actually use it. Usually, at this point in my code I get an error saying, TypeError: 'NoneType' object is not callable. This is usually because my getData() returns none. Ref.get() never actually gets the data at that location.