后台 PHP 进程

发布于 2024-12-07 08:11:44 字数 344 浏览 2 评论 0原文

我正在开发一个网站,需要很多后台进程才能运行该网站。例如,队列、视频编码器和一些其他类型的后台进程。目前,我将它们作为 PHP cli 脚本运行,其中包含:

while (true) {

    // some code

    sleep($someAmountOfSeconds);

}

好吧,这些工作正常,一切正常,但我正在考虑将它们设置为守护进程,这将为它们提供一个我可以监视的实际进程 ID,我也可以在其中运行它们后台并且没有一直打开终端。

我想知道是否有更好的方法来处理这些?我也在考虑 cron 作业,但其中一些进程需要每隔几秒循环一次。

有什么建议吗?

I am developing a website that requires a lot background processes for the site to run. For example, a queue, a video encoder and a few other types of background processes. Currently I have these running as a PHP cli script that contains:

while (true) {

    // some code

    sleep($someAmountOfSeconds);

}

Ok these work fine and everything but I was thinking of setting these up as a deamon which will give them an actual process id that I can monitor, also I can run them int he background and not have a terminal open all the time.

I would like to know if there is a better way of handling these? I was also thinking about cron jobs but some of these processes need to loop every few seconds.

Any suggestions?

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

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

发布评论

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

评论(5

却一份温柔 2024-12-14 08:11:44

创建一个可以调用并提问的守护进程似乎是明智的选择。取决于您的托管服务商是否允许这样的事情,特别是如果您要求它每隔几秒就工作一次,那么基于操作系统的服务/守护进程肯定会比其他任何东西都更明智。

Creating a daemon which you can make calls to and ask questions would seem the sensible option. Depends on wether your hoster permits such things, especially if you're requiring it to do work every few seconds, then definately an OS based service/daemon would seem far more sensible than anything else.

你好,陌生人 2024-12-14 08:11:44

您可以在 PHP 中创建一个守护进程,但根据我的经验,这是一项艰巨的工作,而且由于 PHP 的内存管理和错误处理,结果不可靠。

我遇到了同样的问题,我想用 PHP 编写我的逻辑,但让它由一个稳定的程序守护,如果失败,该程序可以重新启动 PHP 脚本,所以我写了 脂肪控制器

它是用 C 语言编写的,作为守护进程运行,并且可以运行 PHP 脚本,或者实际上任何东西。如果 PHP 脚本由于某种原因结束,胖控制器将重新启动它。这意味着您不必关心守护进程或错误恢复 - 一切都为您处理。

Fat 控制器还可以做很多其他事情,例如并行处理,这是队列处理的理想选择,您可以在此处阅读一些潜在的用例:

http://fat-controller.sourceforge.net/use-cases.html

You could create a daemon in PHP, but in my experience this is a lot of hard work and the result is unreliable due to PHP's memory management and error handling.

I had the same problem, I wanted to write my logic in PHP but have it daemonised by a stable program that could restart the PHP script if it failed and so I wrote The Fat Controller.

It's written in C, runs as a daemon and can run PHP scripts, or indeed anything. If the PHP script ends for whatever reason, The Fat Controller will restart it. This means you don't have to take care of daemonising or error recovery - it's all handled for you.

The Fat Controller can also do lots of other things such as parallel processing which is ideal for queue processing, you can read about some potential use cases here:

http://fat-controller.sourceforge.net/use-cases.html

金橙橙 2024-12-14 08:11:44

我已经使用 PHP 来运行后台任务 5 年了,这与使用任何其他语言执行此操作没有什么不同。只需使用 CRON 和锁定文件即可。锁定文件将阻止脚本的多个实例运行。

监视代码也很重要,为了防止过时的锁定文件阻止脚本运行,我总是做的一项检查是使用第二个 CRON 作业来检查锁定文件是否早于几分钟以及 PHP 脚本的实例是否存在正在运行,如果没有运行,则会删除锁定文件。

使用此技术可以让您将 CRON 设置为每分钟运行一次脚本,不会出现任何问题。

I've done this for 5 years using PHP to run background tasks and its no different to doing in any other language. Just use CRON and lock files. The lock file will prevent multiple instances of your script running.

Also its important to monitor your code and one check I always do to prevent stale lock files from preventing scripts to run is to have second CRON job to check if if the lock file is older than a few minutes and if an instance of the PHP script is running, if not it then removes the lock file.

Using this technique allows you to set your CRON to run the script every minute without issues.

心意如水 2024-12-14 08:11:44

使用 PEAR 中的 System::Daemon 模块。

Use the System::Daemon module from PEAR.

山人契 2024-12-14 08:11:44

一种解决方案(我真的需要自己尝试一下,因为我可能需要它)是使用 cron,但让该过程循环五分钟左右。然后,让 cron 每五分钟启动一次。当一个死亡时,下一个应该完成(或接近完成)。

请记住,两者可能会有些重叠,因此您需要确保这不会导致冲突(例如写入同一视频文件)。一些简单的进程间通信可能很有用,即使它只是写入临时目录中的 PID 文件。

这种方法技术含量较低,但有助于避免 PHP 长期挂在内存上 - 类似于内置任务重新启动!

One solution (that I really need to try myself, as I may need it) is to use cron, but get the process to loop for five mins or so. Then, get cron to kick it off every five minutes. As one dies, the next one should be finishing (or close to finishing).

Bear in mind that the two may overlap a bit, and so you need to ensure that this doesn't cause a clash (e.g. writing to the same video file). Some simple inter-process communication may be useful, even if it is just writing to a PID file in the temp directory.

This approach is a bit low-tech but helps avoid PHP hanging onto memory over the longer term - sort of in-built task restarts!

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