21 _ = unicode |
21 _ = unicode |
22 |
22 |
23 from functools import partial |
23 from functools import partial |
24 import zmq |
24 import zmq |
25 |
25 |
|
26 from cubicweb.server.cwzmq import cwproto_to_zmqaddr |
26 |
27 |
27 # XXX hack to overpass old zmq limitation that force to have |
28 # XXX hack to overpass old zmq limitation that force to have |
28 # only one context per python process |
29 # only one context per python process |
29 try: |
30 try: |
30 from cubicweb.server.cwzmq import ctx |
31 from cubicweb.server.cwzmq import ctx |
31 except ImportError: |
32 except ImportError: |
32 ctx = zmq.Context() |
33 ctx = zmq.Context() |
33 |
34 |
34 class ZMQRepositoryClient(object): |
35 class ZMQRepositoryClient(object): |
35 """ |
36 """ |
36 This class delegate the overall repository stuff to a remote source. |
37 This class delegates the overall repository stuff to a remote source. |
37 |
38 |
38 So calling a method of this repository will results on calling the |
39 So calling a method of this repository will result on calling the |
39 corresponding method of the remote source repository. |
40 corresponding method of the remote source repository. |
40 |
41 |
41 Any raised exception on the remote source is propagated locally. |
42 Any raised exception on the remote source is propagated locally. |
42 |
43 |
43 ZMQ is used as the transport layer and cPickle is used to serialize data. |
44 ZMQ is used as the transport layer and cPickle is used to serialize data. |
44 """ |
45 """ |
45 |
46 |
46 def __init__(self, zmq_address): |
47 def __init__(self, zmq_address): |
|
48 """A zmq address provided here will be like |
|
49 `zmqpickle-tcp://127.0.0.1:42000`. W |
|
50 |
|
51 We chop the prefix to get a real zmq address. |
|
52 """ |
47 self.socket = ctx.socket(zmq.REQ) |
53 self.socket = ctx.socket(zmq.REQ) |
48 self.socket.connect(zmq_address) |
54 self.socket.connect(cwproto_to_zmqaddr(zmq_address)) |
49 |
55 |
50 def __zmqcall__(self, name, *args, **kwargs): |
56 def __zmqcall__(self, name, *args, **kwargs): |
51 self.socket.send_pyobj([name, args, kwargs]) |
57 self.socket.send_pyobj([name, args, kwargs]) |
52 result = self.socket.recv_pyobj() |
58 result = self.socket.recv_pyobj() |
53 if isinstance(result, BaseException): |
59 if isinstance(result, BaseException): |