doufan3408 2018-08-16 00:44
浏览 40

PHP - 共享主机环境上的长时间运行过程

I have a question which I wasn't able to resolve. I'm developing a script, and I have a long running process, in a nutshell I'm scraping some data from external urls and after that I am adding that data to database, and that takes about 30 minutes or so.

I have used @ini_set( "max_execution_time", 1800 ); to allow the script to run long enough.

Something like this:

function calculator_get_games() {
   @ini_set( "max_execution_time", 1800 );

   $api = Api::getInstance();

   $api::get_all_games();
}

That works on my localhost. The problem is that, of course, doesn't work of shared hosting environment. The script brakes after certain amount of minutes and I get an 503 Error.

The strange thing is that I think it is not max_execution_time related since the script runs for a few minutes before it brakes, and max_execution_time on the server is set to 60 seconds. I read somewhere that I can use set_time_limit() function, to reset the counter or prolong the execution time of the script, but that didn't help, I got the same error after a while. I have also read that some companies on shared hostings set a limit for how long a PHP script can run for, non related to max_execution_time.

The thing is that the script could be used by various users and I wouldn't have control over the servers. So I'm hoping to find a way, to allow the script to run even on shared hostings like the one described above, if possible, so the users could use the script without issues on almost any hosting environments.

So, any help is appreciated. Any idea or alternative approach I can use.

Thanks.

EDIT:

Like Jamie_D Pointed out, I wasn't clear enough. I'm already using WordPress Cron in the background to start the script and update it every day. But it brakes at the same time as when i try to execute the script from the browser. I'm developing a plugin that a user could use on any server basically. On plugin activation, WP cron will start the scraping for data with file_get_contents and return the data after 30 minutes, which will be feed to the database. Hope that clarifies it a bit, sorry for the confusion, i'm still quite a beginner in some of these things.

  • 写回答

1条回答 默认 最新

  • dongtuan5367 2018-08-16 01:04
    关注

    If you need to execute a script that takes that long, I would consider a background process that does not rely on the calling script. Since you have not clarified your application intention it's not possible to point you in the right direction.

    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器