- 論壇徽章:
- 0
|
有時候?qū)憄ython關(guān)于網(wǎng)絡(luò)的程序。比如用urllib2等module發(fā)http請求的時候,發(fā)現(xiàn)有時候會有死掉的情況,就是程序沒任何反應(yīng),也不是cpu,內(nèi)存沒資源的問題。具體情況還沒搞明白那里出的問題,但是找到一個解決辦法。就是設(shè)置socket time out,即:如果一個請求超過一定的時間沒有完成,就終止,再次發(fā)起請求。
這個是從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()里面填一個數(shù)值。小心別太小,別正常的請求也不能完成。
本文來自ChinaUnix博客,如果查看原文請點:http://blog.chinaunix.net/u2/60589/showart_2091803.html |
|