Commits

Matt Bone  committed ae4b9f6

this thing is really working (get and put). adding test and profile stubs. really fast when you don't thread the server.

  • Participants
  • Parent commits c4765d0

Comments (0)

Files changed (4)

File memo_client.py

 import socket
 import pickle
 
+from memo_server import PUT, GET
+
+RECV_SIZE = 1024
+
 class MemoServer(object):
     
     def __init__(self, host, port):
 
 
     def put(self, key, value):
-        msg_parts = (key, value)
+        msg_parts = (PUT, key, value)
         #TODO just stream to the socket? does that work with pickle?
         msg_parts_pickled = pickle.dumps(msg_parts)
         self.s.send(msg_parts_pickled)
         self.reconnect()
 
 
-    def get(key):
-        pass
+    def get(self, key):
+
+        # Tell the server what we want:
+        msg_parts_pickled = pickle.dumps((GET, key))
+        self.s.send(msg_parts_pickled)
+        self.s.shutdown(socket.SHUT_WR)
+
+        # Wait for the response:
+        chunks = [] #TODO use a string buffer instead?
+        chunk = -1
+        while chunk != b"":
+            #I don't think this is totally right because I think recv can
+            #get a zero len answer even when the remote side has _not_
+            #closed the connection
+            chunk = self.s.recv(RECV_SIZE)
+            #chunk = self.clientsocket.read()
+            chunks.append(chunk)
+        try:
+            value = pickle.loads(b"".join(chunks))
+        except EOFError:
+            return None
+        
+        self.reconnect()
+
+        return value
 
 
     def __exit__(self, type, value, trace_back):
     
     with MemoServer("mbone-desktop", 8889) as memo_server:
         memo_server.put("asdf", "jkl")
-        #memo_server.put("qwer", "123")
+        memo_server.put("qwer", "123")
 
+        print(memo_server.get("qwer"))
+        print(memo_server.get("qwer"))
 
 
 
-

File memo_profile.py

+"""Profiling the memo server"""
+
+from memo_client import MemoServer
+
+def lots_of_puts():
+    with MemoServer("mbone-desktop", 8889) as memo_server:
+        for i in range(1, 1000):
+            memo_server.put("asdf", i)
+
+def lots_of_puts_and_gets():
+    with MemoServer("mbone-desktop", 8889) as memo_server:
+        for i in range(1, 1000):
+            memo_server.put("asdf", i)
+
+        for i in range(1, 1000):
+            memo_server.get("asdf")
+
+    
+
+if __name__=='__main__':
+    from timeit import Timer
+    t = Timer("lots_of_puts_and_gets()", "from __main__ import lots_of_puts_and_gets")
+    print(t.timeit(number=100))

File memo_server.py

 
 RECV_SIZE = 1024
 
+PUT = 'p'
+GET = 'g'
+
 class Updater(object):
     lock = threading.Lock()
 
 
 
     def run(self):
-        print("run()")
         chunks = [] #TODO use a string buffer instead?
         chunk = -1
         while chunk != b"":
             #get a zero len answer even when the remote side has _not_
             #closed the connection
             chunk = self.clientsocket.recv(RECV_SIZE)
+            #chunk = self.clientsocket.read()
             chunks.append(chunk)
         try:
             msg_parts = pickle.loads(b"".join(chunks))
         except EOFError:
             return
-        self.update_memo_queue(msg_parts[0], msg_parts[1])
+        
+        if msg_parts[0] == PUT:
+            self.update_memo_queue(msg_parts[1], msg_parts[2])
+        if msg_parts[0] == GET:
+            self.fetch_from_memo_queue(msg_parts[1])
 
 
     def update_memo_queue(self, key, value):
             MEMO_QUEUE[key] = []
         MEMO_QUEUE[key].append(value)
         self.lock.release()
+
+
+    def fetch_from_memo_queue(self, key):
+        self.lock.acquire()
+        #TODO make this blocking?
+        try:
+            value = MEMO_QUEUE[key].pop()
+        except (IndexError, KeyError):
+            value = None
+        self.lock.release()
+
+        pickled = pickle.dumps(value)
+
+        self.clientsocket.send(pickled)
+        self.clientsocket.close() #TODO should shutdown?
         
 
+
 def serve(port=8888):
     #from http://docs.python.org/dev/howto/sockets.html
     
         #now do something with the clientsocket
         #in this case, we'll pretend this is a threaded server
         updater = Updater(clientsocket)
-        thread = threading.Thread(target=updater.run)
-        thread.start()
+        #thread = threading.Thread(target=updater.run)
+        #thread.start()
+        updater.run()
 
 if __name__ == '__main__':
     try:

File memo_test.py

+import unittest
+
+class FunctionalTests(unittest.TestCase):
+    """Functional tests for the memo client and server.
+    """
+
+    def test_hmm(self):
+        self.assertEquals(1, 1)
+
+        
+if __name__ == '__main__':
+    unittest.main()