tags:

views:

713

answers:

3

Hello everybody,

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?

+1  A: 

Try using stream_set_timeout() as well.

Greg
Yeah, it would be a solution, but I would like to know if there is a complete non-blocking way to do that, without having to guess a correct amount of time to wait without having the script to die.I'm wondering if this is some kind of bug.
A: 

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:

This function has no effect when PHP is running in safe mode. There is no workaround other than turning off safe mode or changing the time limit in the php.ini.

Residuum
Thank you, but I don't think that the max\_execution\_time is the real problem (I don't get any error about that), I've set it in the php.ini anyway just to be sure, and the script keeps returning.The strangest thing is that if I try to print the output of the fgets() function I get an empty string.
A: 

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?