Jump to content

sleep() vs cron job


mkoga

Recommended Posts

I'm planning on sending some emails 10 seconds apart from each other until an email queue is complete. Does anyone have an opinion on which would be better for performance? I could sleep(10) after each email sent or run a cron job every 10 seconds. I would rather use a cron job,  but I would have to keep the cron job running every 10 seconds 24/7 unless I did an exec('crontab') call.

 

I guess what it basically comes down to is which is better: running one really long php instance or running a bunch of really short php instances?

 

Any thoughts?

Link to comment
Share on other sites

I would definitely use a cron job.

 

well i mean

if you want the emails sent every 10 seconds

and you dont want to run a different

cron job every 10 seconds, you could

run one cron job that will send the emails every

10 seconds

 

The whole point of a cron job is running code repeatedly. If hes running one long script why would he need the cron job? He only needs to run it once!

Link to comment
Share on other sites

better idea is to do this

Make a cron run once every hour or half hour so you aren't over running the server

In the cron it looks like

<?php
#Get the time right now $time
#Get the list of emails to be sent indexed from 0-n 
$i = 0;
#Also get $in_time initalized
while($time < "Time + 59 mins 30 seconds" && EMAILS TO BE SENT){
#Check to make sure at iteration didn't take less than 10 seconds 
if(GET_TIME_NOW-$in_time <10{
sleep(GET_TIME_NOW_$in_time);
}
#now we have waited at least 10 seconds lets send email
#send emails from $i-How many per 10 second period making sure $i++; for each mail sent

#Get the time again to $in_time for the top par of the loop
}
?>

Basic Idea that probably is better since the cron only refreshes on a minute scale vs a second scale you won't be calling as many dead sets.

Link to comment
Share on other sites

Erm.. well a cron is run from the server and a sleep would be via a user script..

it all depends on when you want it to run!

 

and what is for!

 

sure, but the cron would just in turn execute a php script. so i'm thinking have a cron run once a day to execute this little email queue. the email queue would then iterate through each email that needs to be sent, sleep(10) then goes on to the next email. when the queue is complete, the script ends.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.