I'm running into a problem in that urllib2.urlopen
/requests.post
is very occasionally blocking forever on socket.recv
and never returning.
I'm trying to find out why this is happening and address that problem, but in the mean time I wondered if there was a way from preventing it blocking forever?
I already know about the timeout
optional argument for urllib2.urlopen
and socket.setdefaulttimeout
but unfortunately for my use case a timeout isn't a solution as I'm uploading files with POST any timeout value I use would risk interrupting a normal file upload.
I've also seen some solutions using signals, but this will have the same problem as using timeouts for me (and is also out the question because I'm not doing this from the main thread).
Is it possible to timeout only if no data has been sent/received through the socket for a certain amount of time perhaps? Or maybe there's some way I can use select / poll to prevent the deadlock / blocking that I'm experiencing?
If there is a solution using select / poll, how would I go about incorporating this into urllib2.urlopen
/requests.post
?
I also had the idea that if I could send file data through a write type of interface, so I'd control iterating over the file and sending chunks at a time I could probably have enough control to avoid the stalls. I'm not sure how to achieve it though so I asked the question: Upload a file with a file.write interface
UPDATE
It seems I've always had a misconception of the meaning of timeout
in python, it seems it is actually an idle timeout or read/write timeout (probably the first time I've disagreed with Guido). I always thought it was the max amount of time the response should return in - thank you @tomasz for pointing this out!!
But after adding timeout parameters (tested with both urllib2
and requests
) I've come across some really odd and subtle scenarios, possibly mac specific, where the timeout doesn't work correctly which I'm getting more and more inclined to believe is a bug. I'm going to continue to investigate and find out exactly what the issue is. Again thank you tomasz for your help with this!