为特定函数/代码块(而不是整个脚本)设置超时?

发布于 2024-09-13 06:56:38 字数 394 浏览 1 评论 0原文

我有 php 脚本调用 perl 脚本来执行各种操作,有时我会得到它一直持续下去而没有得到响应的情况,这是基于传递给 perl 脚本的变量,我做了很多事情连续的不同的,所以我无法直接调试它,因为我没有来自 perl 的响应...

我真的很想能够将 php 函数或代码块设置为在特定时间后超时秒数..我一直在搜索这个,但还没有找到任何关于如何做到这一点的东西,

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

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

do {

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

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...

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,

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一桥轻雨一伞开 2024-09-20 06:56:38

最好的选择是使用 proc_open,休眠超时时间,然后在进程仍未完成时调用 proc_terminate。

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

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.

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

夏尔 2024-09-20 06:56:38

好吧,我不太确定这个问题是否会根据我的提问方式得到答案,所以我要做的是执行 perl 调用,其中 php 不等待响应并让 perl 将输出写入到文本文件,然后让 php 在指定的秒数后读取此文件,我认为这是最简单的方法,它仅适用于我在本地服务器上运行的一个小应用程序

Well, I'm not so sure this question would have an answer based on how I asked it, so what I am going to do is do the perl call where php doesn't wait for a response and have perl write the output to a text file, then have php read this after specified number of seconds, I think this is the simplest way to do this, its just for a small app i am running on a local server

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文