Commits

ksbuble committed dc7b64d

Add Client.py which establishes a connection with the Socket Server in the calculations process. Then it receives the calculation data from recv()
in chunks of bufsize, until the server sends a chunk of size 0. Then the Client plots the calculation data.

  • Participants
  • Parent commits 692a1c6

Comments (0)

Files changed (2)

File asyncplot/client.py

+r"""Example computation Client."""
+import time
+import numpy as np
+from matplotlib import pyplot as plt
+import threading_server
+import server
+
+
+class Client(object):
+    def __init__(self, Plot, host='localhost', port=server.PlotSocketServer._HOST):
+        self.host = host
+        self.port = port
+        self.plot = Plot()
+        self.plot.plt.clf()
+
+    def run(self):
+        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+        print "Trying to connect to port %i..." % (self.port,)
+        sock.connect((self.host, self.port))
+        print "Connected to port %i." % (self.port,)
+        BUF_SIZE = 200
+        while True:
+            try:
+                datasz = 1
+                data = ''
+                while datasz != 0: 
+                    tmpdata = sock.recv(BUF_SIZE)
+                    datasz = len(tmpdata)
+                    print "tmpdata from Server is %i bytes long." % (datasz,)
+                    data = data + tmpdata
+                v, kw = cPickle.loads(data)
+                self.plot.draw(*v, **kw)
+            except KeyboardInterrupt:
+                sock.send('quit')
+                print "Sent 'quit' to Server."
+                break
+        print "close client socket and shutdown. "
+        sock.close()
+        sock.shutdown(1)
+
+
+
+def run_client():
+    #create the Client instance and pass the Plot object, then call
+    #run().
+    class Plot(threading_server.Plot):
+        def draw(self, x, y):
+            plt.ioff()
+            plt.clf()
+            plt.plot(x, y)
+            print y
+            plt.ion()
+            plt.draw()
+            time.sleep(0.5)     # Pretend this is slow
+
+    c = Client(Plot=Plot)
+    c.run()
+
+
+if __name__ == "__main__":
+    run_client()
+

File asyncplot/example1.py

     c = threading_server.Client(Plot=Plot)
     c.run()
 
+
+
 if __name__ == "__main__":
     run()