I've got two reasons to use a sleep function: first, to automatically send a confirmation email to a client 20 minutes after they contact us. I don't want to use cron jobs because I want it to be exactly 20 minutes (and I'm sick of my web server sending me emails telling me they initiated a cron job.....a new email every 20 minutes!)
Second reason: I've heard of people sending out mass emails using the sleep function. Since my server will only allow 100 emails an hour, I want to use the sleep function to have the script sleep for an hour then continue where it picked up.
My question is this: does it use server resources? Will it slow things down? Are there any other problems with using the sleep function? Thanks in advance!
While a process is sleep
ing it will not consume CPU time, but the process' working set still requires physical memory and/or pagefile to support that process. In other words, the PHP interpreter process needs to keep running. So long as your server has enough RAM this shouldn't be a problem though.
Email delivery times are pretty variable, so you're not going to get an email to someone's inbox in exactly 20 minutes, no matter what you do.
I use a long-running background script -- launched from CLI, instead of apache -- to handle email sending. My application dumps emails into a queue table, which the mailer script polls every 15 seconds. It sleep()s between polls. This way, I only have one script trying to connect to the SMTP server, and sleeping.
That portion of the app has been running successfully with no major issues for the last 2 years. The only annoyance is keeping the script running -- if it goes down for any reason, mail doesn't go out til you bring it back up. But worst case, you could just restart it via cron periodically, e.g. daily.
If I were tackling your problem, I'd simply put a "Send time" column on the queue table, and date it 20 minutes out for these emails. The mailer would then SELECT * FROM mail_queue WHERE send_time <= NOW()
Alternately, you could use a real jobqueue like beanstalkd. I chose the queue table solely to keep my application stack simple.
Know it's a very old thread but some persons might come across, so here is another suggestion.
This will only work if:
- You are on linux server
- You can run commands (like exec, some shared hosting won't allow that)
Instead of sleeping for a long time, which I do think it's a bad practice (for this scenario), you might consider the at command for this.
While cron job is ideal for repetitive stuff if you only want to run a command once at a set time in the future, at
is your best friend.
This is how I'm scheduling emails to be sent X amount of time after a user interaction with the webpage.
Example:
$wait_time=time()+mt_rand(3600,36000);
//wait a random amount of time between 1 and 10 hours
//$wait_time=time()+1200 -if you want 20 minutes exactly.
$cmd_string=escapeshellcmd('php /path/to/script.php '.$par1.' '.$par2.' "'.$par3.'" '.$parX);
exec("echo -e '$cmd_string' | at ".date("Hi M d",$wait_time));
Than you create a script.php which handles all passed parameters and do whatever you want to do.
sleep();
Yes, sleep does use server resources.
Yes, it slow things down by time you set in sleep(...).
There are any other problem when using "sleep".
someone in this page says
Remember that sleep() means "Let PHP time to do some other stuff".
That means that sleep() can be interrupted by signals. That is important if you work with pcntl_signal() and friends.
Instead of server SLEEP, i.e. sleep(3);
I used "client-side" sleep using Javascript:
if (empty($_COOKIE['my_Timer'])) {
setcookie('my_Timer', "blabla", time()+999999, '/');
die('<script>window.setTimeout(\'window.location="'.$_SERVER['REQUEST_URI'].'"; \',3000);</script>');
}