Set timeout for xmlrpclib.ServerProxy

前端 未结 9 1687
轮回少年
轮回少年 2020-12-29 22:33

I am using xmlrpclib.ServerProxy to make RPC calls to a remote server. If there is not a network connection to the server it takes the default 10 seconds to return a socket.

9条回答
  •  臣服心动
    2020-12-29 23:00

    Here is a verbatim copy from http://code.activestate.com/recipes/473878/

    def timeout(func, args=(), kwargs={}, timeout_duration=1, default=None):
        import threading
        class InterruptableThread(threading.Thread):
            def __init__(self):
            threading.Thread.__init__(self)
            self.result = None
    
            def run(self):
                try:
                    self.result = func(*args, **kwargs)
                except:
                    self.result = default
    
        it = InterruptableThread()
        it.start()
        it.join(timeout_duration)
        if it.isAlive():
            return default
        else:
            return it.result
    

提交回复
热议问题