Michael Forbes avatar Michael Forbes committed 0836526

Fixed termination bug with server.
Added rudementary auto-spawn client code (needs work).

Comments (0)

Files changed (2)

asyncplot/client.py

         self.port = port
         self.plot = Plot()
         self.plot.plt.clf()
-        self._BUF_SIZE = 200
+        self._BUF_SIZE = 2**12
 
     def get_data(self):
         sock = self.connect()
         while datasz != 0:
             data.append(sock.recv(self._BUF_SIZE))
             datasz = len(data[-1])
-            print "tmpdata from Server is %i bytes long." % (datasz,)
+            #print "tmpdata from Server is %i bytes long." % (datasz,)
 
-        print "close client socket and shutdown. "
+        #print "close client socket and shutdown. "
         sock.close()
         #sock.shutdown(1)
 
 
     def connect(self):
         sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
-        print "Trying to connect to port %i..." % (self.port,)
+        #print "Trying to connect to port %i..." % (self.port,)
         sock.connect((self.host, self.port))
-        print "Connected to port %i." % (self.port,)
+        #print "Connected to port %i." % (self.port,)
         return sock
         
     def run(self):
             plt.ioff()
             plt.clf()
             plt.plot(x, y)
-            print y
+            #print y
             plt.ion()
             plt.draw()
             #time.sleep(0.5)     # Pretend this is slow
 
     c = Client(Plot=Plot)
-    raw_input('adjust plot and press return')
+    #raw_input('adjust plot and press return')
     c.run()
 
 

asyncplot/server.py

 
 import cPickle
 import socket
+import subprocess
 import threading
 import SocketServer
 
 
 
 class Server(threading.Thread):
-    def __init__(self, host=PlotSocketServer._HOST,
-                 port=PlotSocketServer._PORT):
+    r"""If you pass `spawn_client_command`, then a plot client will be spawned
+    in a subprocess.
+    """
+    def __init__(self,
+                 host=PlotSocketServer._HOST,
+                 port=PlotSocketServer._PORT,
+                 client_cmd=None,
+                 wait_for_client=False):
+        r"""
+        Parameters
+        ----------
+        client_cmd : str
+           If provided, then this command will be spawned in a subprocess (as a
+           list of commands + arguments expected by :func:`subprocess.call`).
+        wait_for_client : Bool
+           If `True`, then pause server to allow the user to adjust plot
+           windows.
+        """
+        self.client_cmd = client_cmd
+        self.wait_for_client = wait_for_client
         self.server = PlotSocketServer(host=host, port=port)
         threading.Thread.__init__(self, target=self.server.serve_forever)
         self.daemon = True               # Don't keep python alive.
 
     def __enter__(self):
-        #mmf.mdb()
         self.start()
+        if self.client_cmd is not None:
+            self.client_process = subprocess.Popen(self.client_cmd)
+        else:
+            self.client_process = None
+        
+        if self.wait_for_client:
+            raw_input("Adjust plot windows and press return to continue...")
+
         return self.server
 
     def __exit__(self, type, value, traceback):
-        #mmf.mdb()
-        self.server.shutdown()
+        self.server.shutdown()      # This does not close the socket
+        if self.client_process is not None:
+            self.client_process.terminate()
+        self.server.server_close()  # This actually closes the socket
         return value is None
 
 
     t = 0
     dt = 0.01
     tic = time.time()
-    with Server() as s:
-        for n in xrange(100000):
+    with Server(client_cmd=['ipython', '--pylab=osx', 'client.py'],
+                wait_for_client=True) as s:
+        for n in xrange(1000000):
             if n % 100000 == 0:
                 print n, time.time() - tic
                 tic = time.time()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.