Dev-Picayune

picayune: of little value or importance

Python xmlrpclib timeout

For my reference, and the reference of anyone who needs to find it, while playing around with the xmlrpclib module, I found myself needing to be able to adjust the timeout. As near as I can tell, if you don’t set a timeout value, it will run forever or at least a long time (I have never been willing to wait long enough to find out). The trick to giving xmlrpclib a timeout on calls to the server is importing and using the socket module. The following code example essentially covers what you can do:

 import xmlrpclib import socket s = xmlrpclib.Server('http://myRemoteServerName/myEntryPoint') s.myProcedureName('hello input')    #this call will not timeout... socket.setdefaulttimeout(10)            #set the timeout to 10 seconds s.myProcedureName('hello input')   #now the call will timout after 10 seconds socket.setdefaulttimeout(None)       #set it back to no timeout value (if you want to) 

The kicker here is just setting the defaulttimeout value in the socket module with the setdefaulttimeout method. If the RPC call times out, it will throw a timeout exception that you’ll need to be able to handle. But at least that’s better than the alternative of not ever timing out.

Technorati Tags: , ,

No comments yet. Be the first.

Leave a reply

For spam filtering purposes, please copy the number 4110 to the field below: