PHP fgets()(或 fread())不会阻塞
我正在尝试通过 PHP 脚本连接到 beanstalkd 服务器并从现有队列中保留作业。 我正在使用 fgets() 函数从守护进程获取响应,期望脚本挂起,除非有工作可用,这是一个示例代码:
set_time_limit(0);
$connection = fsockopen('localhost', 11300);
fwrite($connection, "reserve\r\n");
stream_set_blocking($connection, TRUE);
fgets($connection);
如您所见,我正在尝试强制 fgets() 函数被阻止我将 max_execution_time 设置为 0(意味着没有限制),但一段时间(2 分钟)后脚本返回,没有错误。 我尝试通过 telnet 运行保留命令,但它按预期挂起。 我也尝试使用 fread() 但我得到了相同的行为。
关于如何解决它有什么建议吗?
I'm trying to connect to a beanstalkd server through a PHP script and to reserve jobs from an existing queue.
I'm using the fgets() function to get responses from the deamon, expecting the script to hang unless a job is available, here's a sample code:
set_time_limit(0);
$connection = fsockopen('localhost', 11300);
fwrite($connection, "reserve\r\n");
stream_set_blocking($connection, TRUE);
fgets($connection);
As you can see I'm trying to force the fgets() function to be blocking and I'm setting the max_execution_time to 0 (meaning no limit), but after a while (2mins) the script returns without errors.
I've tried to run the reserve command via telnet and it hangs as exepcted.
I also tried to use fread() but I get the same behviour.
Any suggestion on how to solve it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也尝试使用
stream_set_timeout()
。Try using
stream_set_timeout()
as well.您是否在 php.ini 中设置了该值? 您是否通过 phpinfo() 确认了其正确值? 如果您以编程方式设置它,那么它将不起作用,参见。 http://us.php.net/manual/en/ function.set-time-limit.php:
Have you set the value in php.ini? Have you confirmed its correct value via phpinfo()? If you set it programmatically, then you it will not work, cf. http://us.php.net/manual/en/function.set-time-limit.php:
守护进程可能会在 2 分钟后输出行结束符。 您是否尝试过检查空白字符串并将其循环回来?
It's possible the daemon outputs an end of line character after 2 min. Have you tried to check for a blank string and loop it back?