Reputation: 12017
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!
Upvotes: 3
Views: 7483
Reputation: 88647
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:
<?php
sleep(20);
?>
...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.
Upvotes: 2
Reputation: 3717
People will still be able to access your site. What you'll normally do is have the php script to update the database in crontab and then have apache serve the web requests. Apache will usually have a limit of the number of concurrent connections it can have, so that will be your limit for the number of people who can access the site at the same time.
The only problem you might run into is the sql query locking the table so that the scripts run a query against the same table and have to wait for the first query to finish. Properly structuring the updating query should prevent that.
Upvotes: 2