问题
I'm trying to do some long polling on my site for notifying users of incoming mail. I would be using mysql for database purposes. Would it be safe to check mysql if there's an update to the table? Would this cause the site to slow down as it will constantly look for an update? How would you even check if mysql is updated?
a code would look something like this
while($currentTime <= $lastTime){
usleep(10000)
clearstatcache();
$sql = mysql_query("SELECT time FROM timestamp WHERE $currentTime = timestamp");
$row = mysql_fetch_array($sql);
$currentTime = $row['time'];
}
So basically, it will keep running until theres a new timestamp in the database, but is this efficient where its making the server look into my database every 10seconds?
Thanks
回答1:
I would strongly advise using cron jobs or scheduled tasks for this purpose. http://en.wikipedia.org/wiki/Cron
http://www.developertutorials.com/tutorials/php/running-php-cron-jobs-regular-scheduled-tasks-in-php-172/
Or scheduled tasks if you are on windows.
The approach you suggest will timeout if you dont prevent that, and keep a proces busy. Whereas cron jobs are meant for this type of stuff.
回答2:
You could use AJAX and let the client side poll your server every X second. But you might describe your problem at a more detailed level...
Other thing possible is cron, but you'll have to provide more infos.
来源:https://stackoverflow.com/questions/8595450/checking-mysql-for-long-polling-purposes