php 使脚本执行不受限制
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15776400/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
make script execution to unlimited
提问by TooCooL
I need to run a script into localhost (xampp) which will generate 14400 records and adds them into database, I have set the max_execution_time = 50000000000
, I dont know if I can make it unlimited by setting it to 0 or -1. But I tried this script before with this max_execution_time to 50000000000 and yet it stoped at certain point, I dont know what else could limit the execution time, I have been running this a lot of times and I am tired of waiting and failing again, what should I change before I run this script again and this time to finish the job?
我需要在 localhost (xampp) 中运行一个脚本,它将生成 14400 条记录并将它们添加到数据库中,我已经设置了max_execution_time = 50000000000
,我不知道是否可以通过将其设置为 0 或 -1 来使其不受限制。但是我之前用这个 max_execution_time 到 50000000000 尝试过这个脚本,但它在某个点停止了,我不知道还有什么可以限制执行时间,我已经运行了很多次,我厌倦了等待和再次失败,什么我应该在再次运行此脚本之前进行更改,这次是为了完成工作吗?
回答by hek2mgl
You'll have to set it to zero. Zero means the script can run forever. Add the following at the start of your script:
您必须将其设置为零。零意味着脚本可以永远运行。在脚本的开头添加以下内容:
ini_set('max_execution_time', 0);
Refer to the PHP documentation of max_execution_time
参考 PHP 文档 max_execution_time
Note that:
注意:
set_time_limit(0);
will have the same effect.
会有同样的效果。
回答by Peter Cullen
Your script could be stopping, not because of the PHP timeout but because of the timeout in the browser you're using to access the script (ie. Firefox, Chrome, etc). Unfortunately there's seldom an easy way to extend this timeout, and in most browsers you simply can't. An option you have here is to access the script over a terminal. For example, on Windows you would make sure the PHP executable is in your path variable and then I think you execute:
您的脚本可能会停止,不是因为 PHP 超时,而是因为您用来访问脚本的浏览器(即 Firefox、Chrome 等)超时。不幸的是,很少有一种简单的方法来延长这个超时时间,而且在大多数浏览器中你根本做不到。您在此处可以选择通过终端访问脚本。例如,在 Windows 上,您将确保 PHP 可执行文件在您的路径变量中,然后我认为您执行:
C:\path\to\script> php script.php
Or, if you're using the PHP CGI, I think it's:
或者,如果您使用的是 PHP CGI,我认为它是:
C:\path\to\script> php-cgi script.php
Plus, you would also set ini_set('max_execution_time', 0);
in your script as others have mentioned. When running a PHP script this way, I'm pretty sure you can use buffer flushing to echo out the script's progress to the terminal periodically if you wish. The biggest issue I think with this method is there's really no way of stopping the script once it's started, other than stopping the entire PHP process or service.
另外,您还可以ini_set('max_execution_time', 0);
像其他人提到的那样在脚本中进行设置。当以这种方式运行 PHP 脚本时,我很确定如果您愿意,您可以使用缓冲区刷新来定期将脚本的进度回显到终端。我认为这种方法的最大问题是,除了停止整个 PHP 进程或服务之外,一旦启动脚本,就真的没有办法停止它。