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: , ,

2 comments

2 Comments so far

  1. alt October 23rd, 2009 7:43 am

    that was quite useful. the layout is bugged though, the line of code is way too long and doesn’t fit the screen;)

  2. ScW October 23rd, 2009 12:59 pm

    fixed the layout… sorry about that… I still need to fix it to use scrollable areas better…

Leave a reply

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