为特定功能/代码块(而不是整个脚本)设置超时? [英] Set a timeout for a specific function / block of code (not the whole script)?

查看:94
本文介绍了为特定功能/代码块(而不是整个脚本)设置超时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些PHP脚本,它们调用perl脚本来做各种事情,有时候我会在没有得到响应的情况下得到它,它基于传递给perl脚本的变量,我是连续做了很多不同的事情,所以我不能直接调试它,因为我没有perl的响应...

I have php scripts that call perl scripts to do various things and sometimes I get it where it just goes on and on without getting a response back, this is based on the variable that is being passed to the perl script and I am doing a lot of different ones in succession so I can't get really debug it directly since I don't have a response from perl...

我真的很喜欢只是能够设置一个PHP函数或代码块在超过一定的秒数后超时。我一直在寻找这个,但还没有发现任何事情如何做到这一点,

I would really like to just be able to set a php function or block of code to timeout after a certain number of seconds.. I have been searching on this but haven't found anything yet on how to do this,

我在想像这样可以工作,但我不认为它会动态更新$ time变量,但也许有办法让这个工作?任何建议表示赞赏

I was thinking something like this could work but I don't think it would dynamically update the $time variable, but maybe there is a way to get this to work? Any advice is appreciated

$time = time();
$timeout = $time + 5; //just as an example

do {

// do stuff
} while ($time < $timeout)


推荐答案

最好的办法是使用proc_open,sleep为超时量,然后调用proc_terminate if该过程仍未完成。

Your best bet would be to use proc_open, sleep for your timeout amount and then call proc_terminate if the process still hasn't completed.

请参阅 http://us3.php.net/manual/en/book.exec.php 了解proc_ *系列的详细信息。

See http://us3.php.net/manual/en/book.exec.php for details on the proc_* family.

这篇关于为特定功能/代码块(而不是整个脚本)设置超时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆