Catch script hangs
half.italian at gmail.com
Mon Sep 28 04:45:36 CEST 2009
On Sep 27, 3:40 pm, Bakes <ba... at ymail.com> wrote:
> Due to an ftp server issue, my python script sometimes hangs whilst
> downloading, unable to receive any more data. Is there any way that I
> could have python check, maybe through a thread or something, whether
> it has hanged (or just, if it's still active after 10 seconds, stop
> it?). I have looked at threading but there does not seem to be a stop
> method on threading, which is a problem. Could the lower level thread
> module be a solution?
> I was thinking something like:
> spawn threaded timer, if it gets to 20, close the thread
> attempt download of file
> cancel threaded timer.
> exit thread.
> would that work at all?
I messed around, and came up with this:
import ftplib, socket
def storbinary(self, command, f, blocksize=8192, callback=None,
Override the storbinary method to make the socket.connection()
outside the object, and to set the timeout of the socket
self.conn = self.transfercmd(command)
buf = f.read(blocksize)
if not buf:
if callback: callback(buf)
ftp = MyFTP("ftp.host","user","password")
fname = "FireOnTheMountain.mov"
timeout = 1
with open(fname, 'r') as fi:
#send the extra timeout arg
ftp.storbinary("STOR %s" % fname, fi, timeout=timeout)
print "TIMED OUT!"
#if we shutdown the socket connection, it seems to properly stop
I think that should at least let you set a timeout for sending each
packet that will end the connection gracefully if the timeout is
reached. Not sure if it raises any other problems.
More information about the Python-list