1

I’m looking at ways to send out 1 email every 1 min. I’ve looked at the example below where the top answer is to use PHP sleep() function.

However, I’ve also found suggestions that sleep() might slow down the server.

I’m not looking for exact answers but general approaches would be great.

Community
  • 1
  • 1
user3192948
  • 231
  • 2
  • 22
  • See Vlads answer in the second topic you linked. Don't use sleep. You can come up with a more elegant solution than that by using your greatest asset, your brain. a job queue or a cronjob that executes every 4 minutes would be ideal. you can store pending jobs in your database, then the cron or job queue can read from that list of pending actions. – r3wt Jun 30 '14 at 00:51
  • i assume your limiting the mail sending as your host limits you from sending more mail- wouldn't it be a better idea to change to an account that allows you to send the mail volumes you need, or a seprate mail sending service –  Jun 30 '14 at 01:01
  • @Dagon Or maybe use a cron job with some PHP logic in a script to better manage mail flow? Because `sleep` is the wrong tool for this problem. – Giacomo1968 Jun 30 '14 at 01:07
  • i still think thats a 'hack' to get around a limit –  Jun 30 '14 at 03:19

2 Answers2

3

However, I've also found suggestions that sleep might slow down the server.

Yes, and hitting the pause button on a movie playing on your computer will slow down the duration of the film based on the amount of time you pause the movie.

The purpose of sleep is to put a pause in your script. As described in the official PHP documentation:

Delays the program execution for the given number of seconds.

So yes, it slows down your server. But only on content or pages where sleep is active.

So if this is a fronted script with sleep in it, it slows down the ability for anyone to view content via the PHP script that uses sleep. Place it in the middle of a page where HTML is rendering with a 1 second delay & your page now takes 1 second longer to render.

If this is a backend process only you really know about or trigger, no big deal. It’s a background process anyway so it will just expectedly slow things down in that realm.

That said, let’s look at your core question which is the first sentence of your post:

I’m looking at ways to send out 1 email every 1 min.

Then what you are looking for is a cron job which is a timed job on a Unix/Linux system. An entry for a cron job for something sending mails out every minute might be something like this:

* * * * * /full/path/to/php /full/path/to/your/php/script.php

But that is superficial. It basically just triggers the script.php every minute. Then within your script.php you would have to create some core logic that would control what happens each time it’s triggered. If you are using a database, then maybe you could create a last_sent field where you sent a time stamp of the last time a mail was sent to a recipient and then you act on that. But again, the logic is based on your core needs.

But at the end of the day, I am not too clear how sleep would factor into any of this. Might be worth it to take a step back and better architect your script to fit your needs knowing what cron is, what sleep is & what they are as well are not.

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
1

It is generally done with a separated worker and a queue manager.

That's it: you have a queue manager (i.e. RabbitMQ) that a sending email worker is bound to,

Then when you need to send 10 emails you put all of them to the corresponding queue at once in the script that serves HTTP response. This step is immediate.

Then a worker reads emails one by one and sends them with required delay. This step takes some time but we don't care.

zerkms
  • 249,484
  • 69
  • 436
  • 539