简体   繁体   English

如何在PHP中超过max_execution_time

[英]How to exceed max_execution_time in PHP

In PHP, I want to run a script who need 2 minutes to compute but my max_execution_time is only 30 seconds. 在PHP中,我想运行一个需要2分钟来计算的脚本,但是我的max_execution_time只有30秒。
I can't change my max_execution_time in php.ini nor call shell_exec 我无法在php.ini中更改max_execution_time,也无法调用shell_exec

I already done all the optimization I could think of. 我已经做了所有我能想到的优化。 Any idea? 任何想法?

Even if you can't change the php.ini file itself, you might be able to change options via PHP code with ini_set . 即使您无法更改php.ini文件本身,也可以通过ini_set通过PHP代码更改选项。

You can also try to call set_time_limit inside your code, resetting the current time counter (this doesn't work in safe mode). 您也可以尝试在代码内调用set_time_limit ,以重置当前时间计数器(在安全模式下不起作用)。

If that doesn't work, try to split the task up into multiple sub-tasks and save the temporary results, so that you can perform the task on multiple executions and thus bypass the time limit. 如果这不起作用,请尝试将任务拆分为多个子任务并保存临时结果,以便您可以多次执行任务,从而绕过时间限制。

The "best" option you have is to cut your algorithm into smaller part. 您拥有的“最佳”选项是将算法分成较小的部分。 Each part should execute within 30 second to avoid timeout. 每个部分应在30秒内执行以避免超时。 Here are few ideas. 这里有一些想法。

Using CURL: 使用CURL:

  • [Script Start] Call run.php let is compute for 20s. [脚本开始]调用run.php let计算20s。
  • Save it stat into a database with an ID 将其统计信息保存到具有ID的数据库中
  • Make a CURL call run.php?resumeID=384 [Script finish] 进行CURL调用run.php?resumeID = 384 [脚本完成]
  • [Script Start] retrieve the stat and continue the computation [脚本开始]检索统计信息并继续计算
  • and so on... 等等...

Using CRON task: 使用CRON任务:

  • call run.php every second. 每秒调用一次run.php。 It will pick a task in your database then flag as "in progress". 它将在数据库中选择一个任务,然后将其标记为“进行中”。 So next time run.php is call it will not pick the "in progress task" 因此,下次调用run.php时,它将不会选择“进行中的任务”
  • After 20s of computation, save the stat of your script in database and change the "in progress" flag to false. 经过20秒钟的计算后,将脚本的统计信息保存在数据库中,并将“进行中”标志更改为false。
  • Next call to run.php will pick the task and continue the processing 下次调用run.php将选择任务并继续处理
  • and so on... 等等...

You can also set it in your .htaccess file 您也可以在.htaccess文件中进行设置

Add the following line to your .htaccess file 将以下行添加到您的.htaccess文件中

php_value max_execution_time 200 

使用方式

ini_set('max_execution_time', 100);

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

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