Ok, just found your message in the archives. Thanks very much for that! By way of response--
>>That may be because your question ventures into fairly deep areas of >> networking >> that most folk who are just learning Python(ie readers of this list) >> have probably >> not encountered. True enough:-) I am learning this as I go. >>If you do want to get into depth on Python networking you may find the >> A-Press book Python Network Programming useful - I do regularly :-). I came across it a few times, was not sure if it would be as useful as more general books, but I think I will get it soon. >> However the only information I could see about timeouts there was >>related to socket.settimeout() which ISTR you didn't want to/couldn't >>use... The docs show there is settimeout() and setdefaulttimeout(). I will try to explain below why I did not think I could use these. I might have been wrong... > What is the best practice for checking for network connectivity > errors when making network calls? Is it better to wrap the functions > that make said calls in threads and time them? > Or to use timeout variables for modules like socket? >> Personally if i was doingt that I'd almost certainy put it in a thread >> and apply a timeout within the thread. but not having tried that I >> don't >> know how easy it would be! A friend shared an implementation of this with me that works well. I have attached it. Maybe you will find it useful! > I am trying to figure out the optimal way to make socket connections > (INET) and check for timeouts. The socket module has > settimeout(timeout) > and setdefaulttimeout(timeout). However, so far as I can tell, these > apply > to socket objects. The type of socket connection I want to make is > getfqdn(address). >> I don't understand, getfqdn() returns a domain name not an a socket? Yes. Example: >>> import socket >>> socket.getfqdn("64.233.169.99") 'yo-in-f99.google.com' And therein lies the rub! The main function of the script I have this in is to go through a list of IP addresses, and find their FQDN, and other information. It is at this step, when I get the FQDN, that I wanted to do some find of timeout setting. But since it is *not* a socket object, I cannot use settimeout()... > So I can set the default timeout for socket, but not a socket object > (makes sense so far). I cannot use the getfqdn(address) method on > a socket object, I have to use it on socket. >> Sorry it's not making sense to me, getfqdn takes a host name not a >> socket. Exactly my problem:-) >> setdefaulttimout is a function in the socket module not a method of >> socket. >> Thus you woulfd call that before reating a new socket: Thanks for the clarification, I will try this. >> What are you trying to do? Establish a socket connection to something >> or just do a name check that times out more quickly?(or slowly) Just trying to get the FQDNs from a list of IPs, and want to have some sane error handling in place in case my connection dies during the queries. Thanks!
class ThreadTimeoutError(Exception): pass from threading import Thread class _ThreadedMethod(Thread): def __init__(self, target, args, kwargs): Thread.__init__(self) self.setDaemon(True) self.target, self.args, self.kwargs = target, args, kwargs self.start() def run(self): try: self.result = self.target(*self.args, **self.kwargs) except Exception, e: self.exception = e except: self.exception = Exception() else: self.exception = None def TimeoutThread(timeout=None): def timeoutthread_proxy(method): if hasattr(method, "__name__"): method_name = method.__name__ else: method_name = 'unknown' def timeoutthread_invocation_proxy(*args, **kwargs): worker = _ThreadedMethod(method, args, kwargs) if timeout is None: return worker worker.join(timeout) if worker.isAlive(): raise ThreadTimeoutError( "A call to %s() has timed out" % method_name) elif worker.exception is not None: raise worker.exception else: return worker.result timeoutthread_invocation_proxy.__name__= method_name return timeoutthread_invocation_proxy return timeoutthread_proxy
_______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor