Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sleep function uses server resources?

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!

like image 364
Dustin Avatar asked Mar 04 '10 04:03

Dustin


People also ask

What does sleep () do in C?

sleep() function in C allows the users to wait for a current thread for a specific time. Other operations of the CPU will function properly but the sleep() function will sleep the present executable for the specified time by the thread.

What is the use of sleep function in PHP?

The sleep() function delays execution of the current script for a specified number of seconds. Note: This function throws an error if the specified number of seconds is negative.

How do you use sleep in SAS?

You should use a null DATA step to call the SLEEP function; follow this DATA step with the rest of the SAS program. Using the SLEEP function in this manner enables you to use the CTRL+BREAK attention sequence to interrupt the SLEEP function and to continue with the execution of the rest of your SAS program.


2 Answers

While a process is sleeping 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.

like image 122
Billy ONeal Avatar answered Oct 12 '22 23:10

Billy ONeal


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.

like image 37
Frank Farmer Avatar answered Oct 13 '22 01:10

Frank Farmer