我在一个函数中运行了一个很长的循环,但没有完成所有最终的迭代并在没有给出任何错误的情况下停止了:
function my_function(){
foreach (range(100,999) as $art_id){
$current++;//see bottom flush functions...
outputProgress($current, $art_id );//see bottom flush functions...
// do a lot of stuff on remote URL...
// including download images ,
// scraping HTMl etc ..
}
}
我正在使用 flush 的一些输出进度来跟踪进度
function outputProgress($current, $total) {
// echo "<span style='background:red;font-size:1.5em;color:#fff;'>" . round($current / $total * 100) . "% </span>";
echo "<span style='background:red;font-size:1.5em;color:#fff;'>" . $current .'/'. $total . "% </span>";
myFlush();
sleep(1);
}
和
function myFlush() {
echo(str_repeat(' ', 256));
if (@ob_get_contents()) {
@ob_end_flush();
}
flush();
}
(不用管百分比计算,它现在被禁用,只显示迭代的 ID)
我注意到大部分时间我都在执行循环, 它会在 20-25 次迭代后停止。有时只有 10 个。
我首先怀疑是 time limit 和 max_execution time,所以我添加了:
set_time_limit(99999);
ini_set('max_execution_time', 99999);
function my_function(){
foreach (range(410,499) as $art_id){ // done 500-600
set_time_limit(99999);
ini_set('max_execution_time', 99999);
// do a lot of stuff on remote URL...
// including download images ,
// scraping HTMl etc ..
}
}
如您所见,我已经添加了 INSIDE 和 OUTSIDE 函数本身,以防万一。
但这并没有多大帮助,循环仍然停止。
我的下一个怀疑是内存限制,所以我添加了:
ini_set('memory_limit','128M');
因为我在做wp,所以我也试过了
define('WP_MEMORY_LIMIT', '128M');
但无济于事。经过少量迭代后,scipt 仍然停止。
请注意 - 脚本不会给出任何错误,它只是在某个循环处停止。
编辑我
我已经粘贴了脚本HERE .它实际上是来自 simplehtmldom 的 scrap_slashdot() 函数略作修改lib 包含示例。
它被修改为插入 wordpress 帖子,同时还下载图像并附加它们。
编辑二
使用@Allendar 评论 echo ini_get('memory_limit'); 似乎有效并且设置为 128M..
最佳答案
我已经加快了 usleep(50000); 的速度来进行测试。这段代码在 PHP 5.4 上只需要一秒钟的时间就可以完成,并且不会导致内存泄漏:
ini_set('memory_limit', '32M'); // Force back to default X/W/L/M/AMP
function my_function(){
$current = 0;
foreach (range(100,999) as $art_id){
$current++;
outputProgress($current, $art_id );
}
}
function outputProgress($current, $total) {
echo "<span style='background:red;font-size:1.5em;color:#fff;'>" . $current .'/'. $total . "% </span>";
myFlush();
usleep(50000);
}
function myFlush() {
echo(str_repeat(' ', 256));
if (@ob_get_contents()) {
@ob_end_flush();
}
flush();
}
my_function();
echo memory_get_usage();
我添加了 $current = 0; 以取消 Xdebug 发出的警告。
内存使用仅输出 282304 字节(约 275,69 千字节)。
每个周期等待 1 秒可能会导致脚本在执行时中止。
ini_set('max_execution_time', 0);
.. 会解决这个问题,但不推荐 ;)
如果您仍然发现脚本突然停止,那一定是在您有评论的部分,您写的是代码。该代码可能对 PHP 守护程序的有效负载足够大而导致中止。除此之外,还有一些主机(如果脚本在线)会阻止您设置 ini 值,甚至可能会在 PHP 进程“僵尸化”太久时终止它。
关于PHP - 执行长脚本时可能遇到的障碍,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15761018/
我在使用omniauth/openid时遇到了一些麻烦。在尝试进行身份验证时,我在日志中发现了这一点:OpenID::FetchingError:Errorfetchinghttps://www.google.com/accounts/o8/.well-known/host-meta?hd=profiles.google.com%2Fmy_username:undefinedmethod`io'fornil:NilClass重要的是undefinedmethodio'fornil:NilClass来自openid/fetchers.rb,在下面的代码片段中:moduleNetclass
我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚
我试图获取一个长度在1到10之间的字符串,并输出将字符串分解为大小为1、2或3的连续子字符串的所有可能方式。例如:输入:123456将整数分割成单个字符,然后继续查找组合。该代码将返回以下所有数组。[1,2,3,4,5,6][12,3,4,5,6][1,23,4,5,6][1,2,34,5,6][1,2,3,45,6][1,2,3,4,56][12,34,5,6][12,3,45,6][12,3,4,56][1,23,45,6][1,2,34,56][1,23,4,56][12,34,56][123,4,5,6][1,234,5,6][1,2,345,6][1,2,3,456][123
我有一个在Linux服务器上运行的ruby脚本。它不使用rails或任何东西。它基本上是一个命令行ruby脚本,可以像这样传递参数:./ruby_script.rbarg1arg2如何将参数抽象到配置文件(例如yaml文件或其他文件)中?您能否举例说明如何做到这一点?提前谢谢你。 最佳答案 首先,您可以运行一个写入YAML配置文件的独立脚本:require"yaml"File.write("path_to_yaml_file",[arg1,arg2].to_yaml)然后,在您的应用中阅读它:require"yaml"arg
我的最终目标是安装当前版本的RubyonRails。我在OSXMountainLion上运行。到目前为止,这是我的过程:已安装的RVM$\curl-Lhttps://get.rvm.io|bash-sstable检查已知(我假设已批准)安装$rvmlistknown我看到当前的稳定版本可用[ruby-]2.0.0[-p247]输入命令安装$rvminstall2.0.0-p247注意:我也试过这些安装命令$rvminstallruby-2.0.0-p247$rvminstallruby=2.0.0-p247我很快就无处可去了。结果:$rvminstall2.0.0-p247Search
我遵循了教程http://gettingstartedwithchef.com/,第1章。我的运行list是"run_list":["recipe[apt]","recipe[phpap]"]我的phpapRecipe默认Recipeinclude_recipe"apache2"include_recipe"build-essential"include_recipe"openssl"include_recipe"mysql::client"include_recipe"mysql::server"include_recipe"php"include_recipe"php::modul
当我尝试安装Ruby时遇到此错误。我试过查看this和this但无济于事➜~brewinstallrubyWarning:YouareusingOSX10.12.Wedonotprovidesupportforthispre-releaseversion.Youmayencounterbuildfailuresorotherbreakages.Pleasecreatepull-requestsinsteadoffilingissues.==>Installingdependenciesforruby:readline,libyaml,makedepend==>Installingrub
我在用Ruby执行简单任务时遇到了一件奇怪的事情。我只想用每个方法迭代字母表,但迭代在执行中先进行:alfawit=("a".."z")puts"That'sanalphabet:\n\n#{alfawit.each{|litera|putslitera}}"这段代码的结果是:(缩写)abc⋮xyzThat'sanalphabet:a..z知道为什么它会这样工作或者我做错了什么吗?提前致谢。 最佳答案 因为您的each调用被插入到在固定字符串之前执行的字符串文字中。此外,each返回一个Enumerable,实际上您甚至打印它。试试
如何检查Ruby文件是否是通过“require”或“load”导入的,而不是简单地从命令行执行的?例如:foo.rb的内容:puts"Hello"bar.rb的内容require'foo'输出:$./foo.rbHello$./bar.rbHello基本上,我想调用bar.rb以不执行puts调用。 最佳答案 将foo.rb改为:if__FILE__==$0puts"Hello"end检查__FILE__-当前ruby文件的名称-与$0-正在运行的脚本的名称。 关于ruby-检查是否
//1.验证返回状态码是否是200pm.test("Statuscodeis200",function(){pm.response.to.have.status(200);});//2.验证返回body内是否含有某个值pm.test("Bodymatchesstring",function(){pm.expect(pm.response.text()).to.include("string_you_want_to_search");});//3.验证某个返回值是否是100pm.test("Yourtestname",function(){varjsonData=pm.response.json