I have a Python program that requests data from several API's then runs some machine learning over the data with intensive performance. I'm trying to find a way to keep the loop going, so it runs consistently every 5 mins, and the API gathering functions are run asynchronously, while the machine learning on that data is run once all the API calls have returned the data.
So, I have something which very basically looks like this:
while(True):
try:
api_data_one = loadFromAPIOne()
api_data_two = loadFromAPITwo()
results = calculateResults(api_data_one, api_data_two)
except Exception as e:
print('Exception raised - Uable to complete this iteration', e)
time.sleep(300)
Now because the machine learning takes about a minute or longer to complete, the iterations are taking longer than 5 mins to restart. And the API calls take some time on their own, so running them at the same time would be ideal.
Not having much experience with async and await I am struggling to find where to start. Could you please advise me on where to start?
async/awaitin the first place? The concurrency of what you describe is at most 3, which is perfectly in the domain of what Threads can handle. Especially if your functions are blocking (as currently the API and calculate ones are), even anasyncevent loop will run them in threads anyway.