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