問題描述
mysqli_poll 的文檔有點少.
在示例中,他們創建了 3 個相同的數組,其中包含要檢查的所有 MySQLi 連接,但是如果您閱讀參數說明,那看起來根本不正確.
In the example, they create 3 identical arrays containing all the MySQLi connections to check, but if you read the parameter descriptions, that doesn't look right at all.
在我看來 $read
是一個包含要檢查的連接的數組,但是 $error
和 $reject
應該是未填充的變量如果有錯誤,將由函數填充.對嗎?
It sounds to me like $read
is an array holding the connections to check, but $error
and $reject
should be unpopulated vars that will be filled by the function if there are errors. Is that correct?
當函數返回 >= 1 時會發生什么?你怎么知道哪些連接已經準備好收割"數據了?$read
也被修改了嗎?即減少到實際有數據的設置連接?
What happens when the function returns >= 1? How do you know which connections have data ready to be "reaped"? Is $read
modified too? i.e. reduced to the set connections that actually have data?
最后,sec
和 usec
真的做了什么嗎?如果是這樣,是什么?我嘗試將 sec
設置為 0
并將 usec
設置為 1(我假設這意味著 0 秒 + 1 微秒 = 1 微秒總等待時間)但它當我運行一個大查詢時,它會暫停超過一秒鐘,所以它似乎不會在超時時中止或導致錯誤.它有什么作用?
Lastly, do sec
and usec
actually do anything? If so, what? I tried setting sec
to 0
and usec
to 1 (I assume that means 0 seconds + 1 microsecond = 1 microsecond total wait time) but it pauses for more than a second when I run a big query, so it doesn't seem to abort or cause an error when it times out. What does it do?
推薦答案
TL;DR:您的假設是正確的,但請注意編碼.
mysqli_poll 是一個圍繞套接字選擇的薄型便利包裝器,并且了解 socket_select
的工作原理會讓您大有幫助以了解此功能.
mysqli_poll is a thin convenience wrapper around a socket select, and knowing how socket_select
works will get you a long way toward understanding this function.
mysqli_poll
僅在底層驅動程序為 mysqlnd
時可用,因為只有 MySQL ND 提供對 MySQL 服務器的本機、套接字級訪問.要記住的重要一點是套接字級別"訪問使輪詢成為可能,以及為什么理解套接字選擇對于理解 mysqli_poll
的功能和限制至關重要.
mysqli_poll
is only available when the underlying driver is mysqlnd
, because only MySQL ND provides native, socket-level access to the MySQL server. The important point to keep in mind is that the "socket-level" access is what makes polling possible, and why understanding socket select is crucial in understanding the function and limitations of mysqli_poll
.
回答您的問題:
在我看來,$read 是一個包含要檢查的連接的數組,但 $error 和 $reject 應該是未填充的變量,如果出現錯誤,將由函數填充.對嗎?
It sounds to me like $read is an array holding the connections to check, but $error and $reject should be unpopulated vars that will be filled by the function if there are errors. Is that correct?
是的,但不是完整的圖片.注意mysqli_poll
的簽名:
Yes, but not a complete picture. Notice the signature of mysqli_poll
:
int mysqli_poll ( 數組 &$read , 數組 &$error , 數組 &$reject , int $sec [, int $usec ] )
int mysqli_poll ( array &$read , array &$error , array &$reject , int $sec [, int $usec ] )
所有三個數組都是按引用傳遞的,這意味著 PHP 引擎能夠修改所有三個數組,它會這樣做.在明顯的情況下,當來自 $read
的任何請求連接處于錯誤或連接拒絕狀態時,它會修改 $error
和 $reject
.
All three arrays are pass-by-reference, meaning the PHP engine has the ability to modify all three, which it will. In the obvious case, it modifies $error
and $reject
when any of the requested connections from $read
are in an error or connection rejected state.
但是PHP也會在有數據等待讀取時修改$read
.這是回答您問題的關鍵:
But PHP will also modify $read
when there is data waiting to be read. This is the key to answering your question:
當函數返回 >= 1 時會發生什么?您如何知道哪些連接已準備好收割"數據?$read 也被修改了嗎?即減少到實際有數據的設置連接?
What happens when the function returns >= 1? How do you know which connections have data ready to be "reaped"? Is $read modified too? i.e. reduced to the set connections that actually have data?
是的,這在文檔中至關重要且不明顯.$read
將被修改為準備讀取的連接列表.您將遍歷它們并做您的生意.但是一個必要的點被掩蓋了:如果 $read
被修改,如果你把輪詢放在一個循環中并嘗試再次讀取它們會發生什么?好吧,您只會從一個子集中讀取,這不是您想要的.
Yes, and that is crucial and not obvious in the docs. $read
is going to get modified to the list of connections that are ready to be read. You'll loop over them and do your business. But an imperative point is glossed over: if $read
is modified, what happens if you put the poll in a loop and try to read from those again? Well, you'll only be reading from a subset, which isn't what you want.
在 PHP 中進行選擇時,大多數示例表明,源 $read
數組在被選擇之前被復制到一個新數組.在 mysqli_poll
的手冊頁中,注意這個循環在調用 mysqli_poll 之前重置"讀取數組:
What most examples show when doing a select in PHP is that the source $read
array is copied to a new array before being select'd. In the man page for mysqli_poll
, notice this loop that "resets" the read array just before the call to the mysqli_poll:
foreach ($all_links as $link) {
$links[] = $errors[] = $reject[] = $link;
}
這可能是最重要的一點:當 mysqli_poll
完成時,傳遞到 mysqli_poll
的這些數組中的每一個都會被修改:數組將被修剪,以便只有受影響的連接在結果中,因此您必須在每次調用 mysqli_poll
之前重置數組.
This is perhaps the most important point: each of these arrays passed into mysqli_poll
will get modified when mysqli_poll
finishes: the arrays will be trimmed so that only affected connections are in the result, so you have to reset the arrays each time before you call mysqli_poll
.
另一個例子見這個關于socket_select
的PHP注釋一>.注意到如何在選擇之前 $read = $clients;
嗎?
Another example is seen in this PHP note on socket_select
. Notice how $read = $clients;
before the select?
最后一個問題:
最后,sec 和 usec 真的可以做什么嗎?如果是這樣,是什么?我嘗試將 sec 設置為 0 并將 usec 設置為 1(我假設這意味著 0 秒 + 1 微秒 = 1 微秒總等待時間)但是當我運行一個大查詢時它會暫停超過一秒,所以它似乎沒有中止或超時時導致錯誤.它有什么作用?
Lastly, do sec and usec actually do anything? If so, what? I tried setting sec to 0 and usec to 1 (I assume that means 0 seconds + 1 microsecond = 1 microsecond total wait time) but it pauses for more than a second when I run a big query, so it doesn't seem to abort or cause an error when it times out. What does it do?
是的,它有效.這些應該表示上限 PHP 將等待數據在 $read
中的任何連接上可用(但請繼續閱讀).它對您不起作用,因為最短時間是 1 秒.當您將 0
設置為秒時,即使您有 >0
微秒,PHP 將其解釋為永遠等待".
Yes, it works. These should represent the upper-bound PHP will wait for data to become available on any of the connections in $read
(but read on). It did not work for you because the minimum time is 1 second. When you set 0
for second, even though you had a > 0
microsecond, PHP interpreted that as "wait forever".
作為旁注,mysqli_poll 的單元測試
可能很有啟發性.
As a side note, the unit tests for mysqli_poll
might be illuminating.
更新:昨晚我沒有靠近計算機進行測試.現在,我有一些觀察要分享.
Update: I wasn't near a computer to test last night. Now that I am, I've got some observations to share.
$ cat mysqli_poll_test
$link = mysqli_connect(...);
$sql = 'SELECT SLEEP(2), 1';
mysqli_query($link, $sql, MYSQLI_ASYNC);
$links = array ($link);
$begin = microtime(true);
$i = 0;
do {
printf("start i=%d @ T+%.f
", $i, (microtime(true)-$begin));
$read = $error = $reject = $links;
mysqli_poll($read, $error, $reject, 1, 500000);
printf(
"finish i=%d, count(read, error, reject)=(%d, %d, %d) @ T+%f
",
$i++, count($read), count($error), count($reject), (microtime(true)-$begin)
);
} while (count($links) !== count($read) + count($error) + count($reject));
$ php mysqli_poll_test
start i=0 @ T+0.000012
finish i=0, count(read, error, reject)=(0, 0, 0) @ T+1.501807
start i=1 @ T+1.501955
finish i=1, count(read, error, reject)=(1, 0, 0) @ T+2.001353
在這個測試中,長時間運行的查詢是在 MySQL 服務器上簡單的休眠 2 秒.mysqli_poll
的超時時間為 1.5 秒.正如預期的那樣,經過 1.5 秒后,投票返回.同樣正如預期的那樣,沒有準備好讀取的數據,因此 do .. while
重新啟動.在剩下的半秒后,輪詢返回,表明一個鏈接已準備好讀取.這是意料之中的,因為查詢只需要 2 秒即可解決,而 poll 發現非常接近兩秒.
In this test, the long running query is a simple sleep for 2 seconds at the MySQL server. The mysqli_poll
has a timeout of 1.5 seconds. As expected, after 1.5 seconds elapse, the poll returns. Also as expected, there is no data ready to be read, so the do .. while
restarts. After the remaining half-second, the poll returns indicating one link is ready to read. This is expected, because the query takes only 2 seconds to resolve and poll sees that very close to exactly two seconds.
如果將輪詢超時更改為半秒并重新運行:
If you change the poll timeout to half a second and re-run:
// changed this from 1 to 0 --------V
mysqli_poll($read, $error, $reject, 0, 500000);
投票在半秒后開始,并且循環運行四次,正如預期的那樣.如果像示例中那樣將其更改為 1 微秒,它會在 1 微秒后退出.如果您將其更改為 0 秒和 0 微秒,它會盡可能快地運行.
The poll kicks out after half a second, and the loop runs four times, as expected. If you change it to 1 microsecond as in your example, it does kick out after 1 microsecond. And if you change it to 0 seconds and 0 microseconds, it runs as fast as it possibly can.
所以,當我說 0
意味著永遠等待時,我絕對錯了.
So, I was definitely wrong when I said 0
meant wait forever.
讓我們更改腳本以添加更多鏈接,然后重試:
Let's change our script to have a few more links and try again:
$link0 = mysqli_connect(...);
$link1 = mysqli_connect(...);
$link2 = mysqli_connect(...);
$sql0 = 'SELECT SLEEP(2) AS wait, 1 AS num';
$sql1 = 'SELECT foo FROM';
$sql2 = 'SELECT 2 AS num';
mysqli_query($link0, $sql0, MYSQLI_ASYNC);
mysqli_query($link1, $sql1, MYSQLI_ASYNC);
mysqli_query($link2, $sql2, MYSQLI_ASYNC);
$links = array ($link0, $link1, $link2);
$begin = microtime(true);
$i = 0;
do {
printf("start i=%d @ T+%.f
", $i, (microtime(true)-$begin));
$read = $error = $reject = $links;
$count = mysqli_poll($read, $error, $reject, 1, 500000);
if (0 < $count) {
foreach ($links as $j => $link) {
$result = mysqli_reap_async_query($link);
if (is_object($result)) {
printf("link #%d, row=%s
", $j, json_encode($result->fetch_assoc()));
mysqli_free_result($result);
} else if (false !== $result) {
printf("link #%d, output=%s
", $j, $link);
} else {
printf("link #%d, error=%s
", $j, mysqli_error($link));
}
}
}
printf(
"finish i=%d, count(read, error, reject)=(%d, %d, %d) @ T+%f
",
$i++, count($read), count($error), count($reject), (microtime(true)-$begin)
);
} while (count($links) !== count($read) + count($error) + count($reject));
在此測試中,我希望立即解決兩個結果:一個是語法錯誤,另一個是數據行.我也希望這需要 1.5 秒,因為直到超時到期后才會解決休眠 2 秒的查詢.情況似乎并非如此:
In this test, I'm expecting two results to resolve immediately: one a syntax error and the other a data row. I'm also expecting this to take 1.5 seconds, since the query sleeping 2 seconds will not resolve until after the timeout expires. That doesn't appear to be the case:
start i=0 @ T+0.000002
link #0, row={"wait":"0","num":"1"}
link #1, error=You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
link #2, row={"num":"2"}
finish i=0, count(read, error, reject)=(1, 0, 0) @ T+2.001756
start i=1 @ T+2.001827
finish i=1, count(read, error, reject)=(0, 0, 3) @ T+3.503024
它一直等到 SLEEP(2)
查詢解析,違反了超時是等待上限的斷言.發生這種情況的原因是 mysqli_reap_async_query
:我們正在迭代所有鏈接,并且要求獲取每個鏈接.收割過程一直等到查詢完成.
It waits until the SLEEP(2)
query resolves, violating the assertion that the timeout was the upper-bound for waiting. The reason this happens is the mysqli_reap_async_query
: we're iterating over all links, and each of those is being asked to be reaped. The reaping process waits until the query finishes.
與測試 #2 相同,但這次讓我們對收獲的東西保持清醒.
Same as test #2, but this time let's be smart about what we're reaping.
$ cat mysqli_poll.php
<?php
$link0 = mysqli_connect(...);
$link1 = mysqli_connect(...);
$link2 = mysqli_connect(...);
$sql0 = 'SELECT SLEEP(2) AS wait, 1 AS num';
$sql1 = 'SELECT foo FROM';
$sql2 = 'SELECT 2 AS num';
mysqli_query($link0, $sql0, MYSQLI_ASYNC);
mysqli_query($link1, $sql1, MYSQLI_ASYNC);
mysqli_query($link2, $sql2, MYSQLI_ASYNC);
$links = array ($link0, $link1, $link2);
$begin = microtime(true);
$i = 0;
do {
printf("start i=%d @ T+%.f
", $i, (microtime(true)-$begin));
$read = $error = $reject = $links;
$count = mysqli_poll($read, $error, $reject, 1, 500000);
printf(
"check i=%d, count(read, error, reject)=(%d, %d, %d) @ T+%f
",
$i, count($read), count($error), count($reject), (microtime(true)-$begin)
);
if (0 < $count) {
reap('read', $read);
reap('error', $error);
reap('reject', $reject);
} else {
printf("timeout, no results
");
}
printf("finish i=%d
", $i++);
} while (count($links) !== count($read) + count($error) + count($reject));
function reap($label, array $links) {
foreach ($links as $link) {
$result = mysqli_reap_async_query($link);
if (is_object($result)) {
printf("%s, row=%s
", $label, json_encode($result->fetch_assoc()));
mysqli_free_result($result);
} else if (false !== $result) {
printf("%s, output=%s
", $label, $link);
} else {
printf("%s, error=%s
", $label, mysqli_error($link));
}
}
}
現在運行它.
$ php mysqli_poll.php
start i=0 @ T+0.000003
check i=0, count(read, error, reject)=(1, 0, 0) @ T+0.001007
read, error=You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
finish i=0
start i=1 @ T+0.001256
check i=1, count(read, error, reject)=(1, 0, 1) @ T+0.001327
read, row={"num":"2"}
reject, error=You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
finish i=1
start i=2 @ T+0.001627
check i=2, count(read, error, reject)=(0, 0, 2) @ T+1.503261
timeout, no results
finish i=2
start i=3 @ T+1.503564
check i=3, count(read, error, reject)=(1, 0, 2) @ T+2.001390
read, row={"wait":"0","num":"1"}
reject, error=You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
reject, error=
finish i=3
好多了.每個查詢都會在自己合適的時候解析,并填充適當的數組.與之前的示例相比,此示例中的重要區別在于我們遍歷每個修改過的數組.這與某些文檔相反,這些文檔顯示迭代所有鏈接本身.
Much better. Each query resolves in its own good time, with appropriate arrays filled out. The important difference in this example, versus earlier is that we iterate over each of the modified arrays. This runs contrary to some documentation, which shows iterating over all the links themselves.
我已經在其上打開了文檔錯誤 #70505.
這篇關于mysqli_poll 如何工作?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!