This bug has been killing me a week but I think I have narrowed it down to a strange behavior that I cannot explain. I'm sorry in advance for the long question but I'll try to be as clear as possible.
I'm using fast API and I had a endpoint such as this:
@app.get("/company/{tickers}/dosomething")
async def dosomething(tickers):
query_statement = f"""
select *
from mytable
where ticker in { *tickers* }
"""
#main_db_instance is a module I wrote to wrap my db connect via asyncpg.
result_data = await main_db_instance.fetch_rows(query_statement)
return jsonable_encoder(result_data)
This above works perfect! If you call it directly it works.
I then went step further and created a simple library to make data access easier for users
def requestCompanyReturning(ticker, endpoint):
r = requests.get(f'myurl.com/company/{*ticker*}/{*endpoint*}', *headers*={'Authorization': f'{config.api_key}'})
df = pd.read_json(r.content)
return df
def dosomething(ticker):
df = requestCompanyReturning(*ticker*, "dosomething") #calls above function to get data
return df
dosomething(‘ibm’)
This works when called directly and when I give the library for users to access. Everything work fine so far.
My users started to use the library to build some cool things and it works perfectly on their machine and on a notebook.
I took their code and added a new fastapi endpoint:
@app.get("/company/{tickers}/dosomethingEvenCooler")
async def dosomethingcooler(tickers):
var = dosomething(‘ibm’)
var = #something even cooler than above happens here! Hold on to your hats!
…
return jsonable_encoder(var)
For some reason, not always but at this point, the var is empty. It’s driving me crazy but it’s not all the time. If I add a print statement under it, or sleep, it isn’t as often.
It feels totally random so I cannot figure out the cause but if I run a loop for 500 data points, many will fail but then I run it again and a totally different set will fail.
When I reviewed, it seems like when I call within my fast API app the requests for my requestCompanyReturning library call is empty. It works without any issues on our machines, colab notebooks and everywhere else but randomly on when called within a app it is empty.
Does anyone have any idea?
I checked the logs and do not see any errors related to a failure with the call. In fact I do see the calls from requests being made even when it fails/is empty after.