永久运行PHP脚本,访问队列 [英] Have a PHP script run forever, access a queue

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

问题描述

另请参见永远让PHP脚本循环从队列系统执行计算工作,但这并不能回答我的所有问题.

See also Having a PHP script loop forever doing computing jobs from a queue system, but that doesn't answer all my questions.

如果我想永远运行PHP脚本,请访问队列并执行作业:

If I want to run a PHP script forever, accessing a queue and doing jobs:

  1. 潜在的内存问题是什么?如何避免它们? (是否有任何冲洗函数或我应该使用的东西?)

  1. What is the potential for memory problems? How to avoid them? (any flush functions or something I should use?)

如果脚本由于某种原因而死怎么办?自动重新启动的好方法是什么?

What if the script dies for some reason? What would be a good method to automatically start it up again?

什么是启动脚本的最佳基本方法.由于它永远运行,所以我不需要cron.但是我该如何启动呢? (另请参阅2.)

What would be the best basic approach to start the script. Since it runs forever, I don't need cron. But how do I start it up? (See also 2.)

推荐答案

将队列设置为cron脚本.让它每10秒执行一次.脚本启动后,检查是否存在锁定文件(如.lock之类的东西).如果有,请立即退出.如果不是,请创建.lock并开始处理.如果发生任何错误,请通过电子邮件/日志记录这些错误,删除.lock并退出.如果没有任务,请退出.

Set the queue up as a cron script. Have it execute every 10 seconds. When the script fires up, check if there's a lock file present (something like .lock). If there is, exit immediately. If not, create the .lock and start processing. If any errors occur, email/log these errors, delete .lock and exit. If there's no tasks, then exit.

我认为这种方法是理想的,因为PHP并非真正能够像您要求的那样长时间运行脚本.为了避免潜在的内存泄漏,崩溃等情况,连续执行脚本是一种更好的方法.

I think this approach is ideal, since PHP isn't really designed to be able to run a script for extended periods of time like you're asking. To avoid potential memory leaks, crashes etc, continuously executing the script is a better approach.

这篇关于永久运行PHP脚本,访问队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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