Jump to content

The sleep( ) Function


iwato

Recommended Posts

QUESTION: Why is the first time not echoed before the 10 seconds have elapsed?BACKGROUND: After 10 seconds both echoed statements appear indicating a 10 second difference.

		 <?php		echo date('h:i:s') . "<br />";		sleep(10);		echo date('h:i:s');	   		?>

SOURCE: This is code taken from the W3Schools website.Roddy

Link to comment
Share on other sites

PHP has an output buffer in order to minimize I/O operations between it and the server, and therefore improve performance.In cases like that where you know you'll have a busy backend, you can manually flush the output buffer with flush() and/or ob_flush() before going into the busy backend (in this case, before sleep()-ing).If you want to turn off output buffering, you can use ob_implicit_flush() and/or ob_end_flush() to turn on implicit flushing, i.e. flushing without a buffer, but instead, after every output call. If you go on that route, you'll have to start making sure you have as less "echo and the like" calls as possible, while still progressively loading your page. i.e. you'll have to forget about constructs like:

echo '<table>';echo '<tr>';...echo '</tr>';echo '</table>';

(which I'm guessing are the main reason output buffering, as a concept, exists)

Link to comment
Share on other sites

P.S. if you really do want delays like that, then it's better to use JavaScript, or else the page will still indicate it's loading while it sleeps (and some user agents may not display anything as it waits anyway).

Link to comment
Share on other sites

PHP has an output buffer in order to minimize I/O operations between it and the server, and therefore improve performance.
I have experimented with the sleep( ), flush( ), and op_implicit_flush( ) functions, and they all perform as expected. Although encouraging, my expectations were met under the assumption that the write buffer is a subset of the output buffer. Is this true? Roddy
Link to comment
Share on other sites

if you really do want delays like that, then it's better to use JavaScript, or else the page will still indicate it's loading while it sleeps (and some user agents may not display anything as it waits anyway).
With all of the caveats attached to the PHP website about the flush( ) function, your point is well made.I have even discovered a new one that is not listed. The flush( ) function only flushes to the last completed HTML tag.Roddy
Link to comment
Share on other sites

I have experimented with the sleep( ), flush( ), and op_implicit_flush( ) functions, and they all perform as expected. Although encouraging, my expectations were met under the assumption that the write buffer is a subset of the output buffer. Is this true? Roddy
What write buffer? Are you talking about the one for (file) streams? If so, the output buffer works on pretty much the same premise as that kind of a buffer, but it's not "a subset". A new buffer is created for every opened file, so PHP's output buffer, and each file's output buffer (or, like you call it, a "write buffer") have no interaction with one another.
Link to comment
Share on other sites

What write buffer? Are you talking about the one for (file) streams? If so, the output buffer works on pretty much the same premise as that kind of a buffer, but it's not "a subset". A new buffer is created for every opened file, so PHP's output buffer, and each file's output buffer (or, like you call it, a "write buffer") have no interaction with one another.
QUESTION ONE:I obtained the notion of a write buffer from the functional description of the flush() function on the PHP website.
Flushes the write buffers of PHP and whatever backend PHP is using (CGI, a web server, etc). This attempts to push current output all the way to the browser with a few caveats.
Do the words output buffer and file buffer, as you have used them, correspond to the ob_flush() and flush(), respectively? See below.
flush() may not be able to override the buffering scheme of your web server and it has no effect on any client-side buffering in the browser. It also doesn't affect PHP's userspace output buffering mechanism. This means you will have to call both ob_flush() and flush() to flush the ob output buffers if you are using those.
QUESTION TWO: When I apply the ob_implicit_flush() and set the flag to TRUE, does it inhibit both of the aforementioned buffers or only the output buffer?NOTE: None of my experimentation with buffer flushing has included a file resource that I have created.Roddy
Link to comment
Share on other sites

1. Oh. Well, in that case, the "output buffer" and "write buffer(s) of PHP" are eqivalent terms indeed. It's just that when PHP folk say "the output buffer", that usually implies "the write buffers of PHP", whereas "the write buffer" usually implies any write buffer, including the one of streams.2. As far as I know, it affects only the output buffer.

Link to comment
Share on other sites

... in that case, the "output buffer" and "write buffer(s) of PHP" are eqivalent terms indeed.
Now that I know that the term write buffer is sometimes confused with that of the output buffer, and that the ob_implicit_flush() function operates on the output buffer, why is it that the following two blocks of code produce identical results:CODE BLOCK ONE:
ob_implicit_flush(1);echo "Start time: " . date("h:i:s") . "<br />";sleep(3);echo "End time: " . date("h:i:s");

CODE BLOCK TWO:

echo "Start time: " . date('h:i:s') . "<br /><br />";flush();sleep(3);echo "End time: " .  date('h:i:s');

Is it because both the flush() and ob_implicit_flush() functions both operate on the output buffer?If so, what is an example of a function that operates on a file buffer? Would it be the clearstatcache() function?Roddy

Link to comment
Share on other sites

fflush() and stream_set_write_buffer() are the functions you're looking for.BTW, I found them by simply going to the "fopen" function, and looking into the left pane for something that contains the words "flush" or "buffer"... I hadn't seen those before.
Link to comment
Share on other sites

fflush() and stream_set_write_buffer() are the functions you're looking for.
As always, thank you for responding, but I am still unsure and wish to be clear about my illusions. :) I was once taught that you can appear smart in the moment, not ask the question, and be dumb for a life-time, or appear dumb in the moment, ask the question, and be smart for a life-time. As I have usually found the latter alternative the wiser, please tell me, if the following statements are correct:(True or False) A file buffer is a buffer that is created when a file is opened as a resource using the fopen( ) function. Functions such as the fflush( ) and stream_set_write_buffer( ) functions can only flush the buffers of files that are open resources.(True or False) Output buffers and file buffers are separate entities. You can flush both or either, but flushing either does not flush the other.If false, please explain why.Roddy
Link to comment
Share on other sites

The first is generally true, a file buffer specifically is data waiting to be written to the file which hasn't been written yet. I guess the second is true, but it would be more correct to say that a file buffer is one type of output buffer.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...