python的settimeout

有時候寫python關於網絡的程序。比如用urllib2等module發http請求的時候,發現有時候會有死掉的情況,就是程序沒任何反應,也不是cpu,內存沒資源的問題。具體情況還沒搞明白那裏出的問題,但是找到一個解決辦法。就是設置socket time out,即:如果一個請求超過一定的時間沒有完成,就終止,再次發起請求。
這個是從2.3有的功能用法如下:
settimeout( value)
Set a timeout on blocking socket operations. The value argument can be a nonnegative float expressing seconds, or None. If a float is given, subsequent socket operations will raise an timeout exception if the timeout period value has elapsed before the operation has completed. Setting a timeout of None disables timeouts on socket operations. s.settimeout(0.0) is equivalent to s.setblocking(0); s.settimeout(None) is equivalent to s.setblocking(1). New in version 2.3.
就是settimeout()裏面填一個數值。小心別太小,別正常的請求也不能完成。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章