【散分】如何确保php每次都会执行一个函数

amani11 2010-09-07 09:14:00
例子代码如下,目的很简单,故意让页面执行超时。期望最后会执行doSomething方法

问题如下:
1、程序可以打印出$this->length,但是不是100%,当失败的时候提示echo $this->length;行超时
2、file_put_contents从没执行成功过,为什么呢?
3、我期望程序每次最后都能执行doSomething【这里不用__destruct】,有什么办法呢?

error_reporting(E_ALL);
set_time_limit(rand(1,3));

$tt = new TT(10);
$tt->TODO();

class TT
{
private $length = 0;
function __construct($length = 0)
{
$this->length = $length;
register_shutdown_function(array(&$this, 'doSomething'));
}
function TODO()
{
while($this->length--)
{
sleep(1);
}
}
function doSomething()
{
echo $this->length;
file_put_contents("justTest.txt", $this->length);
}
}
...全文
348 28 打赏 收藏 转发到动态 举报
写回复
用AI写文章
28 条回复
切换为时间正序
请发表友善的回复…
发表回复
luanjian_1234 2010-09-10
  • 打赏
  • 举报
回复
学习,没看懂!
fxs_2008 2010-09-10
  • 打赏
  • 举报
回复
印象中,只有在对象或引用全部销毁才执行。
如果已销毁,如何才能执行其中的方法
w7424 2010-09-10
  • 打赏
  • 举报
回复
Fatal error: Maximum execution time of 3 seconds exceeded in
hero1010 2010-09-10
  • 打赏
  • 举报
回复
Fatal error: Maximum execution time of 1 second exceeded in E:\workspace\date\wenjian\t.php on line 23

Fatal error: Maximum execution time of 1 second exceeded in E:\workspace\date\wenjian\t.php on line 29
blizzf99 2010-09-09
  • 打赏
  • 举报
回复
頂貼收藏。

kaifadi 2010-09-08
  • 打赏
  • 举报
回复
执行了。错误好多啊:
Fatal error: Maximum execution time of 1 second exceeded in E:\phpxuexi\client.php on line 21
9
Warning: file_put_contents() [function.file-put-contents]: open_basedir restriction in effect. File(justTest.txt) is not within the allowed path(s): (e:/phpxuexi) in E:\phpxuexi\client.php on line 26

Warning: file_put_contents(justTest.txt) [function.file-put-contents]: failed to open stream: Operation not permitted in E:\phpxuexi\client.php on line 26
F:
Warning: readfile() [function.readfile]: open_basedir restriction in effect. File(justTest.txt) is not within the allowed path(s): (e:/phpxuexi) in E:\phpxuexi\client.php on line 27

Warning: readfile(justTest.txt) [function.readfile]: failed to open stream: Operation not permitted in E:\phpxuexi\client.php on line 27
kyzy_yy_pm 2010-09-08
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 cunningboy 的回复:]
从命令行运行php thisfile.php,可以输出剩余次数,并且能够将剩余次数写入文件
[/Quote]+
phf0313 2010-09-08
  • 打赏
  • 举报
回复
新手围观学习!
xujian2009 2010-09-08
  • 打赏
  • 举报
回复
呵呵。高手都在啊。学习并接分哈。
gengxuliang 2010-09-08
  • 打赏
  • 举报
回复
超时了
zhangyong20081204 2010-09-08
  • 打赏
  • 举报
回复
记标一个,谢谢
mr_fang 2010-09-08
  • 打赏
  • 举报
回复
噢..散分..我喜欢...
maquan 2010-09-08
  • 打赏
  • 举报
回复
另外对楼主那个 callback 的写法比较感兴趣:
register_shutdown_function(array(&$this, 'doSomething'));

我以前都是用类似下面这样的写法,好像效果应该一样吧?
register_shutdown_function(array($this, 'doSomething'));



--------
With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead.

————————————————————————————————
基于CSDN论坛提供的插件扩展功能,自己做了个签名档工具,分享给大家,欢迎技术交流 :)
maquan 2010-09-08
  • 打赏
  • 举报
回复
我试验的结果更奇怪!

从来没有发生“超时”的错误,其它的程序逻辑都是按照“预期”的方式运行的。我甚至在 TODO() 里构造了一个死循环 while(true); 结果它就一直在跑(我的 CPU 是 4 核的,结果负载就一直是 25%),我都等了 5 分钟了,还没出来呢(PHP 5.2.12 - win32)

■□■□■□■□■□■□■□■
□             □
■  忍以明志 勤以致远  ■
□             □
■□■□■□■□■□■□■□■
基于CSDN论坛提供的插件扩展功能,自己做了个签名档工具,分享给大家,欢迎技术交流 :)
amani11 2010-09-08
  • 打赏
  • 举报
回复
TO falizixun2

你的php.ini配置有问题,,你可以改变php.ini的open_basedir试试
whxxzm 2010-09-08
  • 打赏
  • 举报
回复
register_shutdown_function
这个很好用啊~~~~~
kaifadi 2010-09-07
  • 打赏
  • 举报
回复
路过,不得不看,MARK!
amani11 2010-09-07
  • 打赏
  • 举报
回复
文件的确是可写出来,,,


Fatal error: Maximum execution time of 2 seconds exceeded in F:\www\sess.php on line 21

Fatal error: Maximum execution time of 2 seconds exceeded in F:\www\sess.php on line 25

类似出现这样的提示,第二行就是输出 echo $this->length;的情况,还是会出现。换句话说调用但没执行完毕doSomething

这时候,文件也就不会写了
xuzuning 2010-09-07
  • 打赏
  • 举报
回复
是的,我加了两句
function doSomething()
{
echo $this->length;
echo 'F:' . file_put_contents("justTest.txt", $this->length);
readfile("justTest.txt");
}
每次都有正确的输出
amani11 2010-09-07
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 xuzuning 的回复:]
经测试,没有发现你说的现象

error_reporting(E_ALL); 时,输出正常,显示超时发生在 sleep(1); 的下一行
error_reporting(E_ALL); 时,输出正常
[/Quote]

谢谢测试

但我刷不到10次,就会出现打印不出$this->length;的情况

而且,你测试file_put_contents有输出么?
加载更多回复(7)

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧