php 的睡眠函数

发布于 2024-10-02 23:26:37 字数 238 浏览 0 评论 0原文

作为使用 cron 作业的可能替代方案,我找到了 sleep 函数。我以前从未使用过这个。

如果我告诉我的脚本在某种循环内运行,并且在该循环内我有这样的指令,

# sleeps for 86400 seconds or one day
sleep(86400);

我的脚本会在 1 天后再次启动吗?即使在此期间不再在我的网络浏览器上访问它? 我觉得不可能,但是我来这里请教专家。

As a possible alternative to using cron jobs, I found the sleep function. I have never used this before.

If I tell my script to run inside a kind of loop, and inside that loop I have an instruction like this

# sleeps for 86400 seconds or one day
sleep(86400);

will my script be launched again after 1 day? even if don't access it on my web browser again within that period?
I think is not possible, but I'm here to ask an expert about it.

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

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

发布评论

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

评论(9

小霸王臭丫头 2024-10-09 23:26:37

根据我的经验,以这种方式使用 PHP 的主要问题不是 Web 服务器超时(有一些方法可以处理该问题,但根据服务器和平台的不同而存在困难),而是内存泄漏。

简单的 PHP 代码往往会泄漏大量内存;在我进行一些分析并放置一些 unset 后,我编写的大多数脚本都能够完成数百倍的工作。我永远无法通过这种方式阻止所有的泄漏。我还被告知标准库中存在内存泄漏,如果这是真的,则无法编写在循环中长时间运行的守护程序。

The main problem with using PHP this way is, in my experience, not web server timeouts (there are ways to handle that with difficulty varying on the server and the platform) but memory leaks.

Straightforward PHP code tends to leak a lot of memory; most of the scripts I wrote were able to do hundreds of times as many work after I did some analysis and placed some unsets. And I was never able to prevent all the leaks this way. I'm also told there are memory leaks in the standard library, which, if true, makes it impossible to write daemons that would run for a long time in loops.

善良天后 2024-10-09 23:26:37

该脚本将超时。您需要使用 set_time_limit 设置它,使其不会超时

The script will timeout. You need to set it so that it won't timeout using set_time_limit.

花开半夏魅人心 2024-10-09 23:26:37

我不会这样做我要么使用 cron (即链接)作业,如果它是常规任务或 at(即链接)作业(如果该作业是在脚本运行时添加的)。

例如,cron 允许您每天下午 1 点运行重复作业,而 at 则允许您安排作业暂时运行一次 +1 天。

如果您选择,我已经为 at 队列编写了 PHP 5.3 包装器沿着那条路走。它可以在 GitHub https://github.com/treffynnon/PHP-at-作业队列包装器

I wouldn't do this I would either use a cron (that is a link) job if it is a regular task or an at (that is a link) job if the job is added at the run time of your script.

cron allows you to run a recurring job every day at 1pm for example whereas at allows you to schedule a job to run once for now +1day for example.

I have written a PHP 5.3 wrapper for the at queue if you choose to go down that route. It is available on GitHub https://github.com/treffynnon/PHP-at-Job-Queue-Wrapper

只是在用心讲痛 2024-10-09 23:26:37

还有time_sleep_until()。也许在特定时间醒来更有用......

There is also time_sleep_until(). Maybe more useful to wake up on a specific time...

逆光飞翔i 2024-10-09 23:26:37

如果您通过 Web 浏览器访问该脚本,它将在 30 秒后终止。

如果您在命令行上启动 PHP 脚本,这可能会起作用。

If you access the script through a web browser, it will be terminated after 30 seconds.

If you start the PHP script on the command line, this could work.

茶底世界 2024-10-09 23:26:37

它会起作用,但是你的“启动时间”会受到漂移的影响。假设您的作业需要运行 10 秒,然后休眠 86400,再运行 10 秒,休眠 86400,等等。您在第一天的午夜准确地启动它。在第二天,它将在当天的凌晨 12:00:10 运行3 现在是上午 12:00:20,等等...

您可以在内部进行一些奇特的数学运算来计算出运行需要多长时间,然后从下一个睡眠调用中减去该时间,但在这一点上,为什么不使用 cron 呢?使用 cron 时,脚本将在每次运行后退出,清理所使用的内存和资源。使用 sleep 方法时,您必须非常小心,不要在某处泄漏资源,否则事情最终会陷入停滞。

It would work, but your "startup time" will be subject to drift. Let's say your job takes 10 seconds to run, then sleeps 86400, runs another 10, sleeps 86400, etc.. You start it exactly at midnight on day 1. On Day 2 it'll run at 12:00:10am, on day 3 it's 12:00:20am, etc...

You can do some fancy math internally to figure out how long the run took, and subtract that from the next sleep call, but at the point, why not use cron? With cron the script will exit after each run, cleaning up memory and resources used. With your sleep method, you'll have to be VERY careful that you're not leaking resources somewhere, or things will eventually grind to a halt.

古镇旧梦 2024-10-09 23:26:37

我之前也遇到过类似的问题,发现了一个 php cron 解析类,它可以让你像运行 crons 一样执行 php。如果您无权直接运行 crons,则可以将其与站点上常用的脚本绑定。

我实际上使用这个脚本作为一个更大的 cron 作业脚本的一部分:

  1. 一个 cron 作业每小时
  2. 为每个子 cron 运行一个 xml 文件,并带有类似 cron 的时间组件(即 - * */2 * * * php /home.. )
  3. 如果当前时间符合子 cron 时间组件的标准,则将运行子 cron 脚本
  4. 设置用户界面,以便我不必从主 cron 中手动添加/删除子 cron cronParser

类位于此处

I had a similar problem before and found a php cron parsing class that will allow you to execute php similar to running crons. You can tie it to a commonly accessed script on your site if you don't have access to run crons directly.

I actually use this script as part of a larger cron job script:

  1. a cron job runs every hour
  2. an xml file for each sub-cron with a cron-like time component(i.e.- * */2 * * * php /home..)
  3. the sub-cron script that will run if the current time meets the criteria of the sub-cron time component
  4. a user interface is setup so that I don't have to manually add/remove sub-crons from the main cron

The cronParser class is here.

半暖夏伤 2024-10-09 23:26:37

许多正确答案,但是:使用 sleep() 意味着您的脚本继续运行,并继续使用内存。将默认超时提高到 30 秒是可行的,但同样,这也是个坏主意。我建议你使用 crontasks。

Many correct answers, but: Using sleep() means your script keeps running, and keeps using memory. Raising the default timeout of 30s will work, but again, this is bad idea. I suggest you use crontasks.

妄断弥空 2024-10-09 23:26:37

这就是发明合法 cron 作业的原因。只需使用crontab即可。使用 PHP 脚本来完成此操作将非常不可靠、存在缺陷且时机不佳。

希望这是有见地的。

This is why legitimate cron jobs were invented. Just use crontab. Using a PHP script to do it will be EXTRAORDINARILY unreliable, buggy, and poorly timed.

Hope this is insightful.

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