简体   繁体   English

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

[英]PHP - Long running process on shared hosting environment

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. 我正在开发脚本,并且运行过程很长,总的来说,我是从外部url抓取一些数据,然后将这些数据添加到数据库中,这大约需要30分钟。

I have used @ini_set( "max_execution_time", 1800 ); 我已经使用@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. 脚本在经过一定时间后制动,并且出现503错误。

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. 奇怪的是,我认为这与max_execution_time不相关,因为脚本在刹车之前运行了几分钟,并且服务器上的max_execution_time设置为60秒。 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. 我读到某个地方可以使用set_time_limit()函数来重置计数器或延长脚本的执行时间,但这无济于事,一段时间后我遇到了同样的错误。 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. 我还读到一些共享主机上的公司设置了一个PHP脚本可以运行多长时间的限制,与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. 像Jamie_D指出的那样,我不够清楚。 I'm already using WordPress Cron in the background to start the script and update it every day. 我已经在后台使用WordPress Cron来启动脚本并每天进行更新。 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. 激活插件后,WP cron将开始使用file_get_contents抓取数据,并在30分钟后返回数据,这些数据将被馈送到数据库中。 Hope that clarifies it a bit, sorry for the confusion, i'm still quite a beginner in some of these things. 希望能对此有所澄清,对不起,对于我的困惑,我在某些方面还是很入门的。

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. 由于您尚未弄清申请意图,因此无法为您指明正确的方向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM