summaryrefslogtreecommitdiff
path: root/exam/xmlrpc_clients.py
diff options
context:
space:
mode:
authorPrabhu Ramachandran2011-11-20 21:38:05 +0530
committerPrabhu Ramachandran2011-11-20 21:38:05 +0530
commit13d4aa869a8a49c2ea27ee97b88decfde48639b5 (patch)
tree248ef1ec21672918cf9ebabf351fd164cbe280c7 /exam/xmlrpc_clients.py
parent637db68dca2a909657c151566ad36897a4353999 (diff)
downloadonline_test-13d4aa869a8a49c2ea27ee97b88decfde48639b5.tar.gz
online_test-13d4aa869a8a49c2ea27ee97b88decfde48639b5.tar.bz2
online_test-13d4aa869a8a49c2ea27ee97b88decfde48639b5.zip
ENH: Python server can now run multiple servers
- the SERVER_PORTS is now a list of ports and when you run python_server.py it will run as many servers as desired. - python_server.py now will create multiple servers via multiprocessing. - the xmlrpc_clients.py is changed to deal with these multiple servers. This allows us to handle many incoming requests. These changes allow us to run the online test for many users. We had over 400 simultaneous users and a total of about 650 users using the app with these modifications.
Diffstat (limited to 'exam/xmlrpc_clients.py')
-rw-r--r--exam/xmlrpc_clients.py42
1 files changed, 39 insertions, 3 deletions
diff --git a/exam/xmlrpc_clients.py b/exam/xmlrpc_clients.py
index 5dc51c6..115ee6e 100644
--- a/exam/xmlrpc_clients.py
+++ b/exam/xmlrpc_clients.py
@@ -1,5 +1,41 @@
from xmlrpclib import ServerProxy
-from settings import SERVER_PORT
+from settings import SERVER_PORTS
+import random
+import socket
-# Connect to the python server.
-python_server = ServerProxy('http://localhost:%d'%(SERVER_PORT))
+
+class PythonServer(object):
+ """A class that manages accesing the farm of Python servers and making
+ calls to them such that no one XMLRPC server is overloaded.
+ """
+ def __init__(self):
+ servers = [ServerProxy('http://localhost:%d'%(x)) for x in SERVER_PORTS]
+ self.servers = servers
+ self.indices = range(len(SERVER_PORTS))
+
+ def run_code(self, answer, test_code, user_dir):
+ """See the documentation of the method of the same name in
+ python_server.py.
+ """
+ done = False
+ result = [False, 'Unable to connect to any Python servers!']
+ # Try to connect a few times if not, quit.
+ count = 5
+ while (not done) and (count > 0):
+ try:
+ server = self._get_server()
+ result = server.run_code(answer, test_code, user_dir)
+ except socket.error:
+ count -= 1
+ else:
+ done = True
+ return result
+
+ def _get_server(self):
+ # pick a suitable server at random from our pool of servers.
+ index = random.choice(self.indices)
+ return self.servers[index]
+
+# views.py calls this Python server which forwards the request to one
+# of the running servers.
+python_server = PythonServer()