I have an application which instantiates an xmlrpclib.ServerProxy
once, and then passes it to several threads (web application requests), which all perform XML/RPC calls simultaneously. This works well with python 2.6. With python 2.7, we're getting a lot of errors (ResponseNotReady
, CannotSendRequest
) as soon as we're in a multi-threaded environment.
# This code works well in python 2.6, and breaks in python 2.7.
import xmlrpclib
import thread
proxy = xmlrpclib.ServerProxy("http://localhost:5000/")
def fetch_users():
print proxy.getUsers()
for _ in range(10):
thread.start_new_thread(fetch_users, ())
while(1):
pass
What is the problem here, and is there a thread-safe way of re-using the ServerProxy-object?
We found the cause for the problem: In python 2.6, a TCP connection is created on every XML/RPC method call. Python 2.7, on the other hand, opens a single TCP connection for each ServerProxy-object, and keeps it open (with servers that support keep-alive
). Furthermore, the class is not thread-safe, so that concurrent requests may interfere with each other.
Apparently, the 2.6 version was thread-safe implicitely, as TCP connections do not get reused and all the connection-specific data seems to be kept in non-shared stack variables.
So possible solutions would be:
ServerProxy
object (and implicitely open a TCP connection) for each threadServerProxy
objectIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With