zmqclient.py
brancholdstable
changeset 8746 88c71ad83d47
parent 8572 e54b3bc39011
child 8670 f02139297beb
equal deleted inserted replaced
8507:0c111b232927 8746:88c71ad83d47
       
     1 # copyright 2003-2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
       
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
       
     3 #
       
     4 # This file is part of CubicWeb.
       
     5 #
       
     6 # CubicWeb is free software: you can redistribute it and/or modify it under the
       
     7 # terms of the GNU Lesser General Public License as published by the Free
       
     8 # Software Foundation, either version 2.1 of the License, or (at your option)
       
     9 # any later version.
       
    10 #
       
    11 # CubicWeb is distributed in the hope that it will be useful, but WITHOUT
       
    12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
       
    13 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
       
    14 # details.
       
    15 #
       
    16 # You should have received a copy of the GNU Lesser General Public License along
       
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
       
    18 """Source to query another RQL repository using pyro"""
       
    19 
       
    20 __docformat__ = "restructuredtext en"
       
    21 _ = unicode
       
    22 
       
    23 from functools import partial
       
    24 import zmq
       
    25 
       
    26 
       
    27 # XXX hack to overpass old zmq limitation that force to have
       
    28 # only one context per python process
       
    29 try:
       
    30     from cubicweb.server.cwzmq import ctx
       
    31 except ImportError:
       
    32     ctx = zmq.Context()
       
    33 
       
    34 class ZMQRepositoryClient(object):
       
    35     """
       
    36     This class delegate the overall repository stuff to a remote source.
       
    37 
       
    38     So calling a method of this repository will results on calling the
       
    39     corresponding method of the remote source repository.
       
    40 
       
    41     Any raised exception on the remote source is propagated locally.
       
    42 
       
    43     ZMQ is used as the transport layer and cPickle is used to serialize data.
       
    44     """
       
    45 
       
    46     def __init__(self, zmq_address):
       
    47         self.socket = ctx.socket(zmq.REQ)
       
    48         self.socket.connect(zmq_address)
       
    49 
       
    50     def __zmqcall__(self, name, *args, **kwargs):
       
    51          self.socket.send_pyobj([name, args, kwargs])
       
    52          result = self.socket.recv_pyobj()
       
    53          if isinstance(result, BaseException):
       
    54              raise result
       
    55          return result
       
    56 
       
    57     def __getattr__(self, name):
       
    58         return partial(self.__zmqcall__, name)