Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Catch script hangs

Reply
Thread Tools

Catch script hangs

 
 
Bakes
Guest
Posts: n/a
 
      09-27-2009
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:
thread:
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?
 
Reply With Quote
 
 
 
 
exarkun@twistedmatrix.com
Guest
Posts: n/a
 
      09-27-2009
On 10:40 pm, 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?


No. There are a great many issues which arise when trying to forcibly
terminate a thread. Python doesn't expose this functionality because
most platforms don't provide it in a safe or reliable way.

You could give Twisted's FTP client a try. Since it isn't blocking, you
don't need to use threads, so it's easy to have a timeout.

You could also explore solutions based on signal.alarm(). A single-
threaded signal-based solution has some issues as well, but not nearly
as many as a thread-based solution.

Jean-Paul
 
Reply With Quote
 
 
 
 
Sean DiZazzo
Guest
Posts: n/a
 
      09-28-2009
On Sep 27, 3:40*pm, Bakes <ba...@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:
> thread:
> 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

class MyFTP(ftplib.FTP):
def storbinary(self, command, f, blocksize=8192, callback=None,
timeout=0):
"""
Override the storbinary method to make the socket.connection()
object available
outside the object, and to set the timeout of the socket
"""
self.voidcmd('TYPE I')
self.conn = self.transfercmd(command)
self.conn.settimeout(timeout)
while 1:
buf = f.read(blocksize)
if not buf:
break
self.conn.sendall(buf)
if callback: callback(buf)
self.conn.close()


ftp = MyFTP("ftp.host","user","password")


fname = "FireOnTheMountain.mov"
timeout = 1


try:
with open(fname, 'r') as fi:
#send the extra timeout arg
ftp.storbinary("STOR %s" % fname, fi, timeout=timeout)
except socket.timeout:
print "TIMED OUT!"
#if we shutdown the socket connection, it seems to properly stop
the transfer
ftp.conn.shutdown(socket.SHUT_RDWR)

ftp.quit()


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.

~Sean
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
catch doesn't catch a thrown exception Marteno Rodia Java 5 08-05-2009 03:30 AM
catch(...) doesn't catch everything Adam C++ 9 02-02-2006 05:02 PM
Gem hangs => TCPSocket.write hangs Tim Shadel Ruby 1 07-24-2005 06:11 AM
why catch (...) can not catch such exception John Black C++ 8 08-20-2004 02:34 PM
Clear hangs up - & hangs up - & hangs up Sue Bilstein NZ Computing 26 03-07-2004 01:33 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57