問題描述
當 python 線程在網絡調用(HTTPS)中並且發生上下文切換時會發生什麼? (What happens when the python thread is in network call(HTTPS) and the context switch happens?)
我遇到了一個問題,我們編寫的多線程下載應用程序有時會出現 408 錯誤。我們認為這可能是因為用戶增加了線程數,這導致了錯誤。我們認為有可能在網絡調用期間,發生上下文切換,導致線程沒有發送調用成功所需的所有數據包,並且服務器給出 408 超時錯誤。這是可能的還是網絡調用不依賴於上下文切換。
我們正在使用 python 線程和 pycurl 模塊使用 120 個線程下載數據。
參考解法
方法 1:
What context switch are you referring to?
Pycurl is a wrapper around libcurl which is a C library. When any of libcurl's methods are called, Python's global interpreter lock is released. Python runtime may run its own threads while libcurl is performing client‑side processing, or during actual network I/O, but "context switches" aren't attached to network I/O in any way.
This shouldn't affect your troubleshooting however because you should be using a packet capture tool like tcpdump to record your traffic and identify which side is sending what when you are getting the errors so that you can make some reasoned theories as to what might be going on instead of guessing.