pwired Posted May 28, 2018 Share Posted May 28, 2018 Hi, I was fiddling with php sleep(); but it seems it doesn´t work the way it is explained on php.net and w3schools ! Something so simple like this: sleep(3); // sleep 3 seconds echo "wake up"; sleep(3); echo "hello world"; sleep(3); It doesn't work at all ! What it does is echo all the text strings at once and sleep(3); never enters the equation. Digging further I found that it has something to do with output buffer and need to flush the output buffer with ob_flush() That doesn't work neither. It is not that I am in the need for this sleep(); but it is quite frustrating that something so simple doesn't seem to work. I would love to read from somebody how this is done properly. Link to comment Share on other sites More sharing options...
flydev Posted May 28, 2018 Share Posted May 28, 2018 (edited) Try like that : <?php sleep(3); // sleep 3 seconds echo "wake up<br>"; if (ob_get_length()) { ob_end_flush(); flush(); } sleep(3); echo "hello world<br>"; if (ob_get_length()) { ob_end_flush(); flush(); } sleep(3); or with implicit flush : $hello = ['I', 'love', 'ProcessWire', 'the end']; ob_implicit_flush(true); ob_end_flush(); for($i = 0; $i < count($hello); $i++) { echo $hello[$i].'<br>'; sleep(1); } Edited May 28, 2018 by flydev example 2 2 Link to comment Share on other sites More sharing options...
pwired Posted May 28, 2018 Author Share Posted May 28, 2018 Thanks flydev for stepping in. Just uploaded your code to my hoster, what it does is wait for 3 x 3 seconds and then echoes all text at once. Or what I suspect it already echoed all text from start and only shows it after 3 x 3 seconds. Tricky thing this sleep(); ? Link to comment Share on other sites More sharing options...
flydev Posted May 28, 2018 Share Posted May 28, 2018 Yeah check your PHP settings, maybe your have output_buffering = Off in place. Did you tried my second example ? 1 Link to comment Share on other sites More sharing options...
BitPoet Posted May 28, 2018 Share Posted May 28, 2018 1 hour ago, pwired said: It is not that I am in the need for this sleep(); but it is quite frustrating that something so simple doesn't seem to work. I would love to read from somebody how this is done properly. It's very, very bad practice to use sleep() in a server side script, that is why documentation on working around the pecularities of PHP's buffering (and thread execution) is so sparse. sleep() blocks the whole thread it is called in, and under heavier load, it makes servers utterly unresponsive. There also fine differences in buffering behaviour depending on how PHP is executed (mod_php, FastCGI, classic CGI...) The usual solution to situations where you are tempted to call sleep() is to poll until the condition you need to wait for has been reached, either using classic reloads for the current page or with AJAX. Nowadays, websockets are also used to split off parts of the work to different processes and notify the "owning" session of changes of state in the backend. 7 Link to comment Share on other sites More sharing options...
pwired Posted May 28, 2018 Author Share Posted May 28, 2018 Hi flydev yes I tried your second code also. Same thing, all text is outputted or shown at once at the end. I have this setting on my hoster: output_buffering = 4096 Anyways thanks everybody for stepping in on this. Things are not always as they are (simple) explained on php.net and w3schools and a lot of fuzz is behind of it. But luckily 99% is working as they explain. There must remain some challenge right ? ? Oh and just for the record, this is what my hoster writes about output buffering: (I'll keep it for a rainy day) ; Output buffering is a mechanism for controlling how much output data ; (excluding headers and cookies) PHP should keep internally before pushing that ; data to the client. If your application's output exceeds this setting, PHP ; will send that data in chunks of roughly the size you specify. ; Turning on this setting and managing its maximum buffer size can yield some ; interesting side-effects depending on your application and web server. ; You may be able to send headers and cookies after you've already sent output ; through print or echo. You also may see performance benefits if your server is ; emitting less packets due to buffered output versus PHP streaming the output ; as it gets it. On production servers, 4096 bytes is a good setting for performance ; reasons. ; Note: Output buffering can also be controlled via Output Buffering Control ; functions. ; Possible Values: ; On = Enabled and buffer is unlimited. (Use with caution) ; Off = Disabled ; Integer = Enables the buffer and sets its maximum size in bytes. ; Note: This directive is hardcoded to Off for the CLI SAPI ; Default Value: Off ; Development Value: 4096 ; Production Value: 4096 output_buffering = 4096 Link to comment Share on other sites More sharing options...
teppo Posted July 28, 2018 Share Posted July 28, 2018 On 5/28/2018 at 2:59 PM, BitPoet said: It's very, very bad practice to use sleep() in a server side script, that is why documentation on working around the pecularities of PHP's buffering (and thread execution) is so sparse. sleep() blocks the whole thread it is called in, and under heavier load, it makes servers utterly unresponsive. There also fine differences in buffering behaviour depending on how PHP is executed (mod_php, FastCGI, classic CGI...) The usual solution to situations where you are tempted to call sleep() is to poll until the condition you need to wait for has been reached, either using classic reloads for the current page or with AJAX. Nowadays, websockets are also used to split off parts of the work to different processes and notify the "owning" session of changes of state in the backend. Old thread and this is a bit off-topic, but this bit caught my attention, so I thought I'd mention it. The Link Checker module I released some time ago (it was never quite finished, hopefully I'll get a chance to work more on it soon...ish) makes use of a crawler script that is intended to run as a background process and (u)sleeps between external requests. Reading this made me wonder if I should rethink my strategy, but a quick Google search for "is php sleep always bad" resulted in this Stack Overflow thread discussing valid use cases for sleep – and sure enough, the first one raised was "some kind of a crawler". I guess there's at least the off chance that what I'm doing is not entirely evil ? That being said, though, I'll have to agree that there are very few valid use cases for sleep, and most situations could no doubt be better handled using a different approach. I'm also pretty sure that Link Crawler is the first piece of production code I've ever used sleep or usleep for ? 1 Link to comment Share on other sites More sharing options...
Recommended Posts