How to execute long running tasks in PHP without cron

雨燕双飞 提交于 2019-12-08 07:02:25

问题


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 ?


回答1:


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.




回答2:


On your page you could have a database value i.e. script_runninng then check if the value = 0, send the command, if = 1 do nothing. Then make sure you set the value to 1 at the beginning and 0 at the end of your command.




回答3:


You are can use CURL for this task. In database or textfile save time last run script and one day in week run this script (of course, update date before execute script):

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,"http://www.exaple.com/service_script.php");

curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);

curl_setopt($ch, CURLOPT_TIMEOUT, 1);

$result_curl = curl_exec($ch);

curl_close($ch);



来源:https://stackoverflow.com/questions/6385369/how-to-execute-long-running-tasks-in-php-without-cron

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!