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()裏面填一個數值。當心別過小,別正常的請求也不能完成。php

相關文章
相關標籤/搜索