睡眠功能占用服务器资源吗?

发布于 2024-08-23 22:38:58 字数 287 浏览 2 评论 0原文

我使用睡眠功能有两个原因:首先,在客户联系我们 20 分钟后自动向他们发送确认电子邮件。我不想使用 cron 作业,因为我希望它恰好是 20 分钟(而且我厌倦了我的网络服务器向我发送电子邮件,告诉我他们启动了 cron 作业......每 20 分钟就有一封新电子邮件! )

第二个原因:我听说有人使用睡眠功能发送大量电子邮件。由于我的服务器每小时只允许发送 100 封电子邮件,因此我想使用休眠功能让脚本休眠一小时,然后继续接收到的位置。

我的问题是:它使用服务器资源吗?它会减慢速度吗?使用睡眠功能还有其他问题吗?提前致谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

守不住的情 2024-08-30 22:38:58

当进程处于睡眠状态时,它不会消耗 CPU 时间,但进程的工作集仍然需要物理内存和/或页面文件来支持该进程。换句话说,PHP解释器进程需要保持运行。只要您的服务器有足够的 RAM,这就不成问题。

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.

予囚 2024-08-30 22:38:58

电子邮件发送时间变化很大,因此无论您做什么,您都不会在 20 分钟内收到电子邮件到某人的收件箱。

我使用一个长时间运行的后台脚本(从 CLI 启动,而不是 apache)来处理电子邮件发送。我的应用程序将电子邮件转储到队列表中,邮件程序脚本每 15 秒轮询一次。它在轮询之间休眠。这样,我只有一个脚本尝试连接到 SMTP 服务器,然后休眠。

该应用程序的这一部分在过去两年中一直成功运行,没有出现重大问题。唯一的烦恼是保持脚本运行——如果它因任何原因宕机,邮件不会发出,直到您将其恢复。但最坏的情况是,您可以通过 cron 定期(例如每天)重新启动它。

如果我要解决您的问题,我只需在队列表中添加一个“发送时间”列,并为这些电子邮件注明 20 分钟后的日期。然后,邮件程序将SELECT * FROM mail_queue WHERE send_time <= NOW()

或者,您可以使用真正的作业队列,例如 beanstalkd。我选择队列表只是为了让我的应用程序堆栈保持简单。

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.

暗恋未遂 2024-08-30 22:38:58

知道这是一个非常古老的线程,但有些人可能会遇到,所以这里有另一个建议。

这只有在以下情况下才有效:

  • 你在 Linux 服务器上
  • 你可以运行命令(例如 exec,一些共享主机不允许这样做)

而不是长时间休眠,我确实认为这是一个不好的做法(对于这种情况),您可以考虑使用 at 命令。

如果您只想在将来的设定时间运行一次命令,那么 cron 作业非常适合重复性工作,但 at 是您最好的朋友。

这就是我安排在用户与网页交互后 X 时间发送电子邮件的方式。

示例:

$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));

然后您创建一个 script.php 来处理所有传递的参数并执行您想做的任何操作。

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.

思慕 2024-08-30 22:38:58

我没有使用服务器睡眠,即 sleep(3);,而是

使用 Javascript 使用“客户端”睡眠:

if (empty($_COOKIE['my_Timer'])) {

   setcookie('my_Timer', "blabla",  time()+999999, '/');

die('');

}

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>');

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文