Reputation: 136
I am developing a web application, which needs to spawn a process per user. This is done when they navigate to a certain page. I have an AJAX call that calls a PHP script (which then sits and reads data items from the database/session) and goes to sleep, like this:
<?php
session_start();
while($_SESSION['continue']) {
// Do some processing
session_write_close();
sleep(30);
session_start();
}
exit();
When the user navigates away from the page the 'continue' session variable is set to false and the php process above ends.
The processing being carried out is not significant. Reads a few records from database and checks somethings.
Question: Is this a good idea? Will it cause any obvious memory issues when there are lots of users online?
Many thanks, Arvy
Upvotes: 1
Views: 131
Reputation: 7983
I would say this is not a good idea, for a number of reasons.
First of all, if you are running on Apache, which you most likely are, you are going to run out of Apache processes, because it uses one process per request. Your script is a request which will take a very long time to finish, and so with each user logged in there's less processes available, ultimately resulting in a complete lockdown following a gradually more noticeable slowdown.
It is also very easy to abuse this, in case an attacker finds out you are using this - a DoS / DDoS keeping sessions alive would be deadly to your website / server.
It might slow down the users' browsers, not a great deal, but it is a request that will never ever be processed completely, unless you are planning to keep outputting data to the client, which is not a good idea either. AJAX tends to use a greater amount of smaller pull requests, as they are less of a pain to control and they are not as demanding for the browsers.
If you are going with this, and I advise you not to, you need a set_time_limit(0);
at the beginning of the script anyway, as your code would automatically be killed by PHP.
To suggest an alternative, have the AJAX request something of the browser periodically (e.g. 30 seconds as you wanted) - the PHP script gets called every 30 seconds and you can do your processing, taking minimal amount of processing time.
I assume this is for something like last user activity stat / who is online for your website. If you take a look at how websites like Facebook do this, you will see that the browser keeps sending 'pull' requests to a server, and in return, the server sends information like new notifications, messages, online user list, news feed entries, etc.
Upvotes: 1
Reputation: 191789
I'm not really sure what you're ultimate goal is, but sleep
is non-blocking and ideally that should be where the bottleneck of the script is. You shouldn't have to worry about CPU if all other things are fast.
However, each request will add a process to the table and since the processes never (or rarely) exit, your process table will fill up and you will eventually not be able to serve any more requests -- thus this is not a good idea.
Instead what you may want to do is go through the processing on each request. I would expect users to make requests about every 30 seconds. Another possible alternative is to use a cron job that runs every minute and acts on logged in users, or to have some other daemon that you can signal when a user logs in that does the processing for logged in users on a schedule.
Upvotes: 1