如何在没有cron的PHP中执行长时间运行的任务 [英] How to execute long running tasks in PHP without cron

查看:178
本文介绍了如何在没有cron的PHP中执行长时间运行的任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们想象一下我的情况(当然是假的)...



我有一个拥有1000个活跃用户的网站。我需要做一些会慢(数据库清理,备份)等的东西。例如,它可能需要5分钟。



我想完成当用户打开我的网站和一些params被执行(例如,他是一个星期的第一个访客)...用户以某种方式发送信号到服务器 - 这个缓慢的过程必须立即运行,但他不等待它执行(那几分钟)...他只看到通知服务器正在进行一些更新或任何。在此期间打开我的网站的任何其他用户也会看到该通知。



当过程被执行时 - 网站返回到正常行为。


解决方案

检查执行操作员。他们可能是你要找的。
这样的东西

  $ execute =`/ path / to / php / script.php` 

//如果需要输出
echo $ execute;

一旦这个脚本运行,设置一个标志(可以在数据库中或者简单写入文件或者其他的东西)。当脚本结束时,请删除此标记。



在所有其他页面中 - 选中此标记。如果它是ON,然后显示消息。


Lets imagine my situation (it's fake, of course)...

I have web-site that have 1000 active users. I need to do some stuff that will be slow (database clean-up, backups) etc.. For example, it may take 5 minutes.

I want to accomplish that when user opens my web-site and some params are executed (for example, he is first visitor in one week)... user somehow sends signal to server - that slow process must be run now, but he doesn't wait for it's execution (those several minutes)... he just sees notification that server is making some updates or whatever. Any other user that opens my web-site in that time also sees that notification.

When process is executed - web-site returns to it normal behavior. And it all happens automatically.

Is this possible without cron ?

解决方案

Check Execution Operators. They may be what you are looking for. Something like this

$execute = `/path/to/php/script.php`;

// If you needed the output
echo $execute; 

Once this script runs, set a flag (could be in the database or a simple write in a file or something). When the script ends, delete this flag.

In all your other pages - check this flag. If it is ON then display the message.

这篇关于如何在没有cron的PHP中执行长时间运行的任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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