I'm working on a project for my company. Essentially we are going to have a long running process that will hit APIs on a python server to send and recieve JSON responses. It will take the data from these API calls and update tables in a mysql database. It will be responsive, but also perform scheduled tasks.
The code is written and it functions properly. The question is, how do you execute this intelligently? While being memory conscious.
Much like this question I am at the moment, very regretfully and in spite of my many reservations, using a while(true)
loop to run infinitely with a sleep of 15 seconds after everything finishes. The referenced question didn't have very many viable answers and is nearly 3 years old. Any suggestions on how to execute this in a way that will be memory friendly and intelligent.
The question is, how do you execute this intelligently? While being memory conscious.
Doesn't really sound like much is wrong with this approach. If you want to make sure you don't have any memory leaks, make sure you allocate all of your objects with a local scope to ensure it doesn't accumulate to cause an out-of-memory error.
I would also recommend a daemon supervisor something like Supervisor to auto-restart your PHP process if it crashes.
An alternative to this entire approach is to remove your while
loop and auto-launch the PHP process (via CRON job or similar) every few minutes, and let it exit as soon as it's done. The problem with this is that now you have to worry about preventing two or more of your scripts from running in parallel.