I would like run a PHP script that runs every second to update a MySQL table.
Suppose this script takes 0.99 seconds, will this prevent website visitors from accessing the website while the script is executed? Will they receive timeouts/internal server errors?
If so, is there a way around this using perhaps some sort of multi-threaded option?
To give more details, I’m trying to create a role playing game that has monsters in it.
These monsters are stored in a table called rpg_monsters. The monsters table has a ID, target, x and y field and need to look if they are in range of each other to attack possible hostiles.
If they are in range of each other, they need to update their target to that monster ID. Today I still have few monsters, but that number will increase in the future and thus increase execution time of the script.
I understand that in normal cases, you would require a dedicated server for this type of intense work. However, I would like to see how far I can push PHP/MySQL before becoming overloaded.
Thank you in advance!
Short answer: no.
Every new call to a PHP script creates a new instance of PHP (usually a new process, in some cases it’s just run under a new thread, but this does not affect your question), and multiple instances run independently of each other.
As a casing point, try running this script:
…and sending a new request/starting another instance while it is running. You will see that the one does not affect the other in any way.
To add a little more info about the threading element of the question: PHP itself cannot be multi-threaded. If you run it as an IIS module in a recent IIS (off the top of my head I can’t remember exactly when this starts) the PHP instance will be run under a new thread of the IIS process – but you cannot start a new thread from within PHP. In every other setup (AFAIK) every new PHP instance creates a new process.