document x.1.0 changes
[mdref/mdref-pq] / pq / Connection / flush.md
1 # bool pq\Connection::flush()
2
3 Flush pending writes on the connection.
4 Call after sending any command or data on a nonblocking connection.
5
6 If it returns FALSE, wait for the socket to become read or write-ready.
7 If it becomes write-ready, call pq\Connection::flush() again.
8 If it becomes read-ready, call pq\Connection::poll(), then call pq\Connection::flush() again.
9 Repeat until pq\Connection::flush() returns TRUE.
10
11 > ***NOTE:***
12 > This method was added in v1.1.0, resp. v2.1.0.
13
14 ## Params:
15
16 None.
17
18 ## Returns:
19
20 * bool, whether everything has been flushed.
21
22 ## Throws:
23
24 * pq\Connection\InvalidArgumentException
25 * pq\Connection\RuntimeException when no asynchronous operation is active, or flushing failed
26
27 ## Example:
28
29 <?php
30 $c = new pq\Connection();
31 $c->nonblocking = true;
32
33 $c->execAsync("SELECT '".str_repeat("a", 6e7)."'", function($r) {
34 $r->fetchCol($s);
35 var_dump(strlen($s));
36 });
37
38 $flushed = $c->flush();
39 do {
40 while (!$flushed || $c->busy) {
41 $r = $c->busy ? [$c->socket] : null;
42 $w = !$flushed ?[$c->socket] : null;
43
44 if (stream_select($r, $w, $e, null)) {
45 if ($r) {
46 printf("P%d", $c->poll());
47 }
48 if ($w) {
49 printf("F%d", $flushed = $c->flush());
50 }
51 }
52 }
53 echo "\n";
54 } while ($c->getResult());
55 ?>
56
57 ### Yields:
58
59 F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0
60 ... (omitted)
61 F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F1P3P3P3P3P3P3P3P3
62 ... (omitted)
63 P3P3P3P3P3P3P3P3P3P3P3P3P3P3
64 int(60000000)