Skip to content
60 changes: 53 additions & 7 deletions howto/sockets.po
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgstr ""
"Project-Id-Version: Python 3.11\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2022-06-10 00:16+0000\n"
"PO-Revision-Date: 2023-08-03 18:11+0800\n"
"PO-Revision-Date: 2023-08-12 15:16+0800\n"
"Last-Translator: Jay <weijay0804@gmail.com>\n"
"Language-Team: Chinese - TAIWAN (https://github.com/python/python-docs-zh-"
"tw)\n"
Expand Down Expand Up @@ -209,7 +209,7 @@ msgid ""
"allocated port which will be recycled when the conversation ends."
msgstr ""
"事實上,有三種方法可以讓這個迴圈運作 - 分配一個執行緒 (thread) 來處理 "
"``clientsocket`` 、建立一個新行程 (process) 來處理 ``clientsocket``,或者將"
"``clientsocket`` 、建立一個程序 (process) 來處理 ``clientsocket``,或者將"
"這個程式重新改寫成使用非阻塞 socket,並使用 ``select`` 在我們的「伺服器端」"
"socket 和任何有效的 ``clientsocket`` 之間進行多工處理。稍後將會更詳細的介紹。"
"現在最重要的是理解:這就是「伺服器端」socket 做的\\ *所有* \\事情。它不會發送任何"
Expand All @@ -231,7 +231,7 @@ msgid ""
"a shortcut around a couple of layers of network code and be quite a bit "
"faster."
msgstr ""
"如果你需要在一台機器上的兩個行程間進行快速的行程間通訊 (IPC),你應該考慮使用"
"如果你需要在一台機器上的兩個程序間進行快速的程序間通訊 (IPC),你應該考慮使用"
"管道 (pipes) 或共享記憶體 (shared memory)。如果你確定要使用 AF_INET sockets,"
"請將「伺服器端」socket 綁定到 ``'localhost'``。在大多數平台上,這樣將會繞過幾個"
"網路程式碼層,並且速度會更快一些。"
Expand All @@ -240,7 +240,7 @@ msgstr ""
msgid ""
"The :mod:`multiprocessing` integrates cross-platform IPC into a higher-level "
"API."
msgstr ":mod:`multiprocessing` 將跨平台的行程間通訊整合到更高層的 API 中。"
msgstr ":mod:`multiprocessing` 將跨平台程序間通訊整合到更高層的 API 中。"

#: ../../howto/sockets.rst:134
msgid "Using a Socket"
Expand Down Expand Up @@ -519,7 +519,7 @@ msgstr ""

#: ../../howto/sockets.rst:302
msgid "When Sockets Die"
msgstr ""
msgstr "Sockets 何時銷毀"

#: ../../howto/sockets.rst:304
msgid ""
Expand All @@ -535,10 +535,17 @@ msgid ""
"automatic recycling of resources. In other words, if you do manage to kill "
"the thread, your whole process is likely to be screwed up."
msgstr ""
"使用阻塞式 socket 最糟糕的地方可能是在另一端突然強制關閉(未執行 ``close``)"
"的情況下會發生什麼?你的 socket 很可能會處於阻塞狀態。TCP 是一種可靠的協議,"
"它在放棄連線之前會等待很長很長的時間。如果你正在使用執行緒,整個執行緒基本上"
"已經無法使用。在這方面,你無法做太多事情。只要你不做一些愚蠢的事情,比如在"
"執行阻塞式讀取時持有一個鎖,那麼執行緒並不會消耗太多資源。*不要*\\ 試圖終止"
"執行緒 - 執行緒比程序更有效的部分原因是它們避免了與自動回收資源相關的開銷。換"
"句話說,如果你確實設法終止了執行緒,整個程序可能會出現問題。"

#: ../../howto/sockets.rst:318
msgid "Non-blocking Sockets"
msgstr ""
msgstr "非阻塞的 Sockets"

#: ../../howto/sockets.rst:320
msgid ""
Expand All @@ -547,6 +554,9 @@ msgid ""
"calls, in much the same ways. It's just that, if you do it right, your app "
"will be almost inside-out."
msgstr ""
"如果你已經理解了前面的內容,你已經知道了大部分關於使用 sockets 的機制的所需知"
"識,你仍然會以非常相似的方式使用相同的函式。就這樣而已,如果你做的對,你的程式"
"就會是近乎完美的。"

#: ../../howto/sockets.rst:325
msgid ""
Expand All @@ -557,6 +567,11 @@ msgid ""
"the exact same idea. You do this after creating the socket, but before using "
"it. (Actually, if you're nuts, you can switch back and forth.)"
msgstr ""
"在 Python 中可以使用 ``socket.setblocking(False)`` 來設定為非阻塞。在 C 的作"
"法更為複雜(例如,你需要在 BSD 風格的 ``O_NONBLOCK`` 和幾乎沒有區別的 POSIX 風"
"格的 ``O_NDELAY`` 之間做出選擇,這與 ``TCP_NODELAY`` 完全不同),但基本思想是"
"一樣的,你要在建立 socket 後但在使用它之前執行此操作。(實際上,如果你願意的"
"話,你甚至可以來回切換。)"

#: ../../howto/sockets.rst:332
msgid ""
Expand All @@ -567,17 +582,24 @@ msgid ""
"will grow large, buggy and suck CPU. So let's skip the brain-dead solutions "
"and do it right."
msgstr ""
"主要的機制差異在於 ``send``、``recv``、``connect`` 和 ``accept`` 可能在沒有執"
"行任何操作的情況下就回傳了。你當然有多種選擇。你可以檢查回傳值和錯誤代碼,但"
"這些操作通常會讓自己抓狂。如果你不相信我,不妨試試看。你的應用程式會變得臃"
"腫、錯誤百出,並且占用 CPU。所以,讓我們跳過無腦的解決方案,使用正確的方式。"

#: ../../howto/sockets.rst:339
msgid "Use ``select``."
msgstr ""
msgstr "使用 ``select``。"

#: ../../howto/sockets.rst:341
msgid ""
"In C, coding ``select`` is fairly complex. In Python, it's a piece of cake, "
"but it's close enough to the C version that if you understand ``select`` in "
"Python, you'll have little trouble with it in C::"
msgstr ""
"在 C 中,編寫 ``select`` 是非常複雜的,但在 Python 中,這很簡單,並與 C 的版"
"本非常類似,如果你理解了 Python 中的 ``select``,在 C 中處理它時也不會有太大"
"的困難: ::"

#: ../../howto/sockets.rst:352
msgid ""
Expand All @@ -589,13 +611,20 @@ msgid ""
"generally a sensible thing to do - give it a nice long timeout (say a "
"minute) unless you have good reason to do otherwise."
msgstr ""
"你傳遞給 ``select`` 三個列表:第一個列表包含你可能想要嘗試讀取的所有 "
"sockets;第二個包含所有你可能想要嘗試寫入的 sockets,最後一個(通常為空)包含"
"你想要檢查錯誤的 sockets。你應該注意,一個 socket 可以同時存在於多個列表中。"
"``select`` 呼叫是阻塞的,但你可以設置超時。通常這是一個明智的做法 - 除非有"
"充分的理由,否則給它一個很長的超時(比如一分鐘)。"

#: ../../howto/sockets.rst:360
msgid ""
"In return, you will get three lists. They contain the sockets that are "
"actually readable, writable and in error. Each of these lists is a subset "
"(possibly empty) of the corresponding list you passed in."
msgstr ""
"作為回傳,你將獲得三個列表。它們包含實際上可讀取、可寫入和出錯的 sockets。這"
"些列表中的每一個都是你傳入的相應列表的子集(可能為空)。"

#: ../../howto/sockets.rst:364
msgid ""
Expand All @@ -606,6 +635,11 @@ msgid ""
"nothing. (Actually, any reasonably healthy socket will return as writable - "
"it just means outbound network buffer space is available.)"
msgstr ""
"如果一個 socket 在輸出的可讀列表中,你可以幾乎確定,在這個業務中我們能夠得到"
"的最接近確定的事情是,對該 socket 的 ``recv`` 呼叫將會回傳一些\\ *內容*。對於可寫"
"列表,也是同樣的想法。你將能夠發送一些 *內容*。也許不是全部,但\\ *一些內容*\\ 總比"
"什麼都沒有好。(實際上,任何比較正常的 socket 都會以可寫的方式回傳 - 這只是意味"
"者「外送網路 (outbound network)」的緩衝空間是可用的。)"

#: ../../howto/sockets.rst:371
msgid ""
Expand All @@ -615,6 +649,10 @@ msgid ""
"it in the potential_writers list. If it shows up in the writable list, you "
"have a decent chance that it has connected."
msgstr ""
"如果你有一個「伺服器端」socket,請將其放在 potential_readers 列表中,如果它在"
"可讀列表中出現,你的 ``accept`` 呼叫(幾乎可以確定)會成功。如果你建立了一個"
"新的 socket 去 ``connect`` 到其他地方,請將它放在 potential_writers 列表中,"
"如果它在可寫列表中出現,那麼他有可能已經連接上了。"

#: ../../howto/sockets.rst:377
msgid ""
Expand All @@ -624,6 +662,9 @@ msgid ""
"problem of determining whether the other end is done, or just busy with "
"something else."
msgstr ""
"實際上,即使是使用阻塞式 socket 的情況下,``select`` 也很方便。這是一種判斷是否"
"會被阻塞的方法之一 - 當緩衝區中有某些內容時, socket 會回傳為可讀。然而,這"
"仍然無法解決判斷另一端是否完成,或者只是忙於其他事情的問題。"

#: ../../howto/sockets.rst:382
msgid ""
Expand All @@ -633,3 +674,8 @@ msgid ""
"differently on Windows. In fact, on Windows I usually use threads (which "
"work very, very well) with my sockets."
msgstr ""
"**可移植性警告**:在 Unix 上,``select`` 同時適用於 sockets 和文件。但請不要"
"在 Windows 上嘗試這麼做,在 Windows 上,``select`` 只適用於 sockets。同時,請"
"注意,在 C 語言中,許多更進階的 socket 選項在 Windows 上有不同的實現方式。實"
"際上,在 Windows 上,我通常會使用執行緒(這非常,非常有效)與我的 sockets 一起"
"使用。"