PHP脚本永远有效:) [英] PHP script that works forever :)

查看:67
本文介绍了PHP脚本永远有效:)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一些想法来做以下事情。我需要一个PHP脚本执行相当长的时间。这是CMS的扩展,只能是PHP。它也不能是命令行脚本,因为应该由只有CMS的标准功能的普通人使用。一种选择是执行cron作业(大多数简单的托管服务器都具有该作业),该作业会经常触发该脚本,因此,不用长时间工作,它可以逐步执行操作,以保留其从一次启动到下一次启动的状态。这并不完美,但我看不到其他解决方案。如果脚本将重定向到其自身,服务器将中断它。

I'm looking for some ideas to do the following. I need a PHP script to perform certain action for quite a long time. This is an extension for a CMS and this can't be anything else but PHP. It also can't be a command line script because it should be used by common people that will have only the standard means of the CMS. One of the options is having a cron job (most simple hostings have it) that will trigger the script often so that instead of working for a long time it could perform the action step by step preserving its state from one launch to the next one. This is not perfect but I can't see of any other solutions. If the script will be redirecting to itself server will interrupt it. What other options can suit?

提前感谢大家!

推荐答案

您所说的是一个守护程序或长时间运行的程序,它等待客户端程序的调用,执行和操作,提供响应,然后继续等待更多的调用。

What you're talking about is a daemon or long running program that waits for calls by client programs, performs and action, provides a response then keeps on waiting for more calls.

您可能对Apache / amp;的形式很熟悉。 MySQL;)无论如何,PHP在这方面通常是可以的,它确实能够对原始套接字以及fork子进程起作用,以同时处理多个请求。

You might be familiar w/ these in the form of Apache & MySQL ;) Anyway PHP is generally OK in this regard, it does have the ability to function over raw sockets as well as fork sub-processes to handle multiple requests simultaneously.

话虽如此,PHP守护程序是YMMV的工具。有些人会说他们做得很好,而像我这样的人会说他们在进程间通信方面存在问题,甚至在过多的unset()调用中也会泄漏内存。

Having said that PHP daemons are a tool where YMMV. Some folks will say they work great, other folks like me will say they have issues w/ interprocess communication and leaking memory even amidst plethora unset() calls.

将无法在共享托管环境上部署任何类型的守护程序。您需要获得更好的服务器程序包或坚持使用基于Cron的解决方案。

Anyway you likely won't be able to deploy a daemon of any type on a shared hosting environment. You'll need to get a better server package or stick with a Cron based solution.

这里是有关编写PHP守护程序的链接

另外,请注意。守护程序有时会崩溃,因此,即使有人通过电源线跳到共享服务器,您仍可能需要存储发生状况的状态:)

Also, one more note. Daemons do crash from time to time and therefore you may still need to store state about whats going on, just in case someone trips over the power cord to your shared server :)

这篇关于PHP脚本永远有效:)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆