Skip to content

Instantly share code, notes, and snippets.

@Petah
Last active August 5, 2021 21:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Petah/fd69978af055e86d64f9066dcbb6d2b7 to your computer and use it in GitHub Desktop.
Save Petah/fd69978af055e86d64f9066dcbb6d2b7 to your computer and use it in GitHub Desktop.
MySQL Poll
<?php
$queryConnection = new \mysqli($host, $username, $password, $database);
$killConnection = new \mysqli($host, $username, $password, $database);
// Execute the query
$queryConnection->query($sql, MYSQLI_ASYNC | MYSQLI_USE_RESULT);
// Poll for results
$start = microtime(true);
do {
$connections = [$queryConnection];
$error = [];
$reject = [];
if ($queryConnection->poll($connections, $error, $reject, 0, 500 * 1000)) {
break;
}
$executionTime = microtime(true) - $start;
if ($executionTime > $maxExecutionTime / 1000) {
$killConnection->query('KILL QUERY ' . $queryConnection->thread_id);
throw new \Exception('Query took too long: ' . round($executionTime, 4));
}
} while (true);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment