I want to execute a function that would run in parallel to the fast api.
That function basically fetch the data from the web and writes it to the database, the data could be million of the records and could took more than 5 minutes.
The API accepts the data and query the db and response to the client.
What I am currently doing right now is
@app.on_event("startup")
async def startup_event():
await Extractor().callit()
class Extractor:
async def callit(self):
# Insertion of the tor ips with the data models
indicators = []
indicators.clear()
for ip in await scrap_web_ips():
if ip == '':
continue
q_indicator = self.check_ip_status(ip)
if q_indicator is None:
indicator = Indicators(indicator=ip, tor=True,
last_seen=get_current_date_in_iso(),
first_seen=get_current_date_in_iso(),
white_list=True if self.get_vt_stats(ip) == 0 else False)
indicators.append(indicator)
else:
print("Updating the lastseen of the indicator.")
q_indicator.last_seen = get_current_date_in_iso()
q_indicator.tor = True
self.session.commit()
print(self.insert_data(indicators))
And than my APIs.
The function is blocking the api calls.
I don't know how to handle this now.