Package qubx :: Module remote_ctl
[hide private]
[frames] | no frames]

Module remote_ctl

source code

Gives outside processes access to the Python interpreter.

Chris Nicolai 2010-2012

A dedicated thread listens on a TCP port (by default, on the loopback interface '127.0.0.1' only).
Client and server alternately send messages, which consist of a verb, e.g. 'EVAL', and depending
on the verb, sdata (string data) such as '2**5', or bytes (binary array data).

Usage (server):

    >>> server = RemoteServer(host='127.0.0.1', port=55384, seek_ports=10, sockid_path=None)

Listens on the first available port in 55384..(55384+10).  Writes the actual port number
to a text file at sockid_path, if provided.  Enqueues requests for processing, but as most
applications require everything to run on the same thread, it requires you to call

    >>> server.process_one()

at frequent intervals, such as on a timer.  To stop the serve                    print script.getvalue()
                    print script.getvalue()[:-1]
r:

    >>> server.stop()


Usage (client):

    >>> client = RemoteProxy(host='127.0.0.1', port=55384, sockid_path=None)

Connects to the server.  Uses the port number in sockid_path, if it exists.

    >>> import numpy
    >>> arr = numpy.array([1,2,3,4,5,6,7,8,9,10]).reshape((2,5))
    >>> print arr
    [[ 1  2  3  4  5]
     [ 6  7  8  9 10]]
    >>> client.set_array(arr, 'arr')
    >>> client.reval("arr[1,2]")
    '8'
    >>> client.rexec("""import numpy
    arr2 = numpy.array(arr, copy=True)
    arr2 *= 2
    arr2 -= 1""")
    >>> local_arr2 = client.get_array('arr2')
    >>> print local_arr2
    [[ 1  3  5  7  9]
     [11 13 15 17 19]]
    >>> client.stop()


Protocol:

Strictly alternating messages, starting with the client.  Certain messages are always multi-line, and terminated with a period on its own line.  This transcript has all the possible interactions (>: client; <: server)

    >: VERSION<tab>1.0
    <: VERSION<tab>1.0
    >: EXEC
    >: import numpy
    >: table = newTable()
    >: .
    <: DONE
    >: EXEC
    >: i fnot (table is None):
    >:     table.setWindowLabel("tutu")
    <: ERROR
    <: Traceback (most recent call last):
    <: [...]
    <:   File "<string>", line 1:
    <:     i fnot (table is None):
    <:          ^
    <: Syntax error: invalid syntax
    <: .
    >: EVAL<tab>t
    <: VAL
    <: '<qti.Table object at 0x04cc2687>'
    <: .
    >: EVAL<tab>nonsense
    <: ERROR
    <: Traceback (most recent call last):
    <: [...more traceback...]
    <: .
    >: ARRAY<tab>3<tab>4<tab>float32<tab>remote_label
    >: [3*4*sizeof(float32) bytes of array data]
    >: .
    <: SET
    >: EVAL<tab>remote_label[1,2]
    <: VAL
    <: 34.6
    <: .
    >: GET ARRAY<tab>remote_label
    <: ARRAY<tab>3<tab>4<tab>float32<tab>remote_label
    <: [3*4*sizeof(float32) bytes of array data]
    <: .

Copyright 2008-2011 Research Foundation State University of New York 
This file is part of QUB Express.                                          

QUB Express is free software; you can redistribute it and/or modify          
it under the terms of the GNU General Public License as published by 
the Free Software Foundation, either version 3 of the License, or    
(at your option) any later version.                                  

QUB Express is distributed in the hope that it will be useful,               
but WITHOUT ANY WARRANTY; without even the implied warranty of       
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        
GNU General Public License for more details.                         

You should have received a copy of the GNU General Public License,   
named LICENSE.txt, in the QUB Express program directory.  If not, see        
<http://www.gnu.org/licenses/>.                                      

Classes [hide private]
  RemoteMsg
Represents one message with associated data.
  RemoteConnect
Reads and writes RemoteMsgs.
  RemoteSession
Handles the server side of one connection.
  RemoteRunner
Manages servant sockets, wraps with RemoteSessions; you call .process_one() to get their work done.
  RemoteServer
Listens on the first available port in port..(port+seek_ports).
  RemoteError
Contains the traceback from an exception on the remote side.
  RemoteController
  RemoteCommandServer
Listens on the first available port in port..(port+seek_ports).
Functions [hide private]
 
RemoteArray(arr, label)
Returns a RemoteMsg containing arr and its label.
source code
 
RemoteToArray(msg)
Returns (label, arr) contained in a RemoteArray message.
source code
 
RemoteString(s, label)
Returns a RemoteMsg containing s and its label.
source code
 
RemoteToString(msg) source code
 
RemoteProxy(host='127.0.0.1', port=55384, sockid_path=None)
Connects to the server.
source code
Variables [hide private]
  DEFAULT_HOST = '127.0.0.1'
  DEFAULT_PORT = 55384
  __package__ = 'qubx'
Function Details [hide private]

RemoteArray(arr, label)

source code 

Returns a RemoteMsg containing arr and its label.

verb = 'ARRAY' sdata = "%(rows)i %(cols)i %(dtype)s %(label)s" bytes = arr.tostring()

RemoteString(s, label)

source code 

Returns a RemoteMsg containing s and its label.

verb = 'ARRAY' sdata = "%(len(s))s %(label)s" bytes = s

RemoteProxy(host='127.0.0.1', port=55384, sockid_path=None)

source code 

Connects to the server. Uses the port number in sockid_path, if it exists.