Michael Forbes avatar Michael Forbes committed 1cb6f5e

Added better methods for setting the default port.

Comments (0)

Files changed (3)

asyncplot/__init__.py

-__all__ = ['Plot', 'Client', 'Server']
+__all__ = ['client', 'server', 'set_default_port', 'set_default_host']
 
-from threading_server import Plot, Client, Server
+import client
+import server
+
+
+def set_default_port(port):
+    server.PlotSocketServer._DEFAULT_PORT = port
+
+
+def set_default_host(host):
+    server.PlotSocketServer._DEFAULT_HOST = host

asyncplot/client.py

 
     def __init__(self, Plotter, host=None, port=None):
         import server           # Do this here to prevent recursive dependence
-        if host is None:
-            host = server.PlotSocketServer._HOST
-        if port is None:
-            port = server.PlotSocketServer._PORT
-        self.host = host
-        self.port = port
+        self.host = server.get_host(host)
+        self.port = server.get_port(port)
         self.plot = Plotter()
         self._BUF_SIZE = 2**12
 
         #sock.shutdown(1)
 
         res = ''.join(data)
-        print("Getting data %g" % (time.time() - tic,))
+#        print("Getting data %g" % (time.time() - tic,))
         return res
 
     def connect(self):
                 data = self.get_data()            
                 tic = time.time()
                 v, kw = cPickle.loads(data)
-                print("Loading pickle %g" % (time.time() - tic,))
+#                print("Loading pickle %g" % (time.time() - tic,))
                 tic = time.time()
                 self.plot.draw(*v, **kw)
-                print("Plotting %g" % (time.time() - tic,))
+#                print("Plotting %g" % (time.time() - tic,))
         finally:
             self.is_alive = False
 

asyncplot/server.py

 import mmf
 
 
+def get_port(port=None):
+    r"""Return `port` or the default value if `port` is `None`."""
+    if port is None:
+        port = PlotSocketServer._DEFAULT_PORT
+    return port
+
+
+def get_host(host=None):
+    r"""Return `host` or the default value if `host` is `None`."""
+    if host is None:
+        host = PlotSocketServer._DEFAULT_HOST
+    return host
+
+
 class PlotRequestHandler(SocketServer.BaseRequestHandler):
     def handle(self):
         self.request.sendall(self.server._get_data())
     -----
     * Add timeouts.
     """
-    _PORT = 9999
-    _HOST = ''
+    _DEFAULT_PORT = 9999
+    _DEFAULT_HOST = ''
 
-    def __init__(self, host=_HOST, port=_PORT):
+    def __init__(self, host=None, port=None):
         self.lock = threading.Lock()  # RLock()??
         self.have_data = threading.Event()
         self.data = None
         self.daemon_threads = True       # Don't keep python alive.
         self.allow_reuse_address = True  # Allow port reuse after termination.
-        SocketServer.TCPServer.__init__(self, (host, port),
+        SocketServer.TCPServer.__init__(self, (get_host(host), get_port(port)),
                                         PlotRequestHandler)
 
     def _get_data(self, block=True):
     """
     def __init__(self,
                  Plotter=None,
-                 host=PlotSocketServer._HOST,
-                 port=PlotSocketServer._PORT,
+                 host=None,
+                 port=None,
                  client_cmd=["ipython", "--pylab", "-c"],
                  wait_for_client=False):
         r"""
         self.Plotter = Plotter
         self.client_cmd = client_cmd
         self.wait_for_client = wait_for_client
-        self.server = PlotSocketServer(host=host, port=port)
+        self.server = PlotSocketServer(host=get_host(host), port=get_port(port))
         threading.Thread.__init__(self, target=self.server.serve_forever)
         self.daemon = True               # Don't keep python alive.
 
                 port=port)
             client_cmd = self.client_cmd + [client_code]
             client_process = subprocess.Popen(client_cmd,
-                                                   # I/O??????
-                                                   )
+                                              # I/O??????
+            )
             return client_process
         
     def __enter__(self):
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.