nonblocking.inc 991 B

12345678910111213141516171819202122232425262728293031323334353637
  1. <?php
  2. function nb_is_readable($stream, $timeout = 1) {
  3. $r = [$stream]; $w = []; $e = [];
  4. return (bool) stream_select($r, $w, $e, $timeout, 0);
  5. };
  6. function nb_is_writable($stream, $timeout = 1) {
  7. $r = []; $w = [$stream]; $e = [];
  8. return (bool) stream_select($r, $w, $e, $timeout, 0);
  9. };
  10. function nb_flush($db, $db_socket) {
  11. while (TRUE) {
  12. if (! nb_is_writable($db_socket)) {
  13. continue;
  14. }
  15. $flush = pg_flush($db);
  16. if ($flush === TRUE) {
  17. break; // All data flushed
  18. } elseif ($flush === FALSE) {
  19. echo "pg_flush() error\n";
  20. break;
  21. }
  22. }
  23. };
  24. function nb_consume($db, $db_socket) {
  25. while (TRUE) {
  26. if (!nb_is_readable($db_socket)) {
  27. continue;
  28. } elseif (!pg_consume_input($db)) {
  29. echo "pg_consume_input() error\n";
  30. break;
  31. } elseif (!pg_connection_busy($db)) {
  32. break; // All data consumed
  33. }
  34. }
  35. };