Show More
@@ -1,65 +1,65 b'' | |||||
1 | """ Defines a dummy socket implementing (part of) the zmq.Socket interface. """ |
|
1 | """ Defines a dummy socket implementing (part of) the zmq.Socket interface. """ | |
2 |
|
2 | |||
3 | #----------------------------------------------------------------------------- |
|
3 | #----------------------------------------------------------------------------- | |
4 | # Copyright (C) 2012 The IPython Development Team |
|
4 | # Copyright (C) 2012 The IPython Development Team | |
5 | # |
|
5 | # | |
6 | # Distributed under the terms of the BSD License. The full license is in |
|
6 | # Distributed under the terms of the BSD License. The full license is in | |
7 | # the file COPYING, distributed as part of this software. |
|
7 | # the file COPYING, distributed as part of this software. | |
8 | #----------------------------------------------------------------------------- |
|
8 | #----------------------------------------------------------------------------- | |
9 |
|
9 | |||
10 | #----------------------------------------------------------------------------- |
|
10 | #----------------------------------------------------------------------------- | |
11 | # Imports |
|
11 | # Imports | |
12 | #----------------------------------------------------------------------------- |
|
12 | #----------------------------------------------------------------------------- | |
13 |
|
13 | |||
14 | # Standard library imports. |
|
14 | # Standard library imports. | |
15 | import abc |
|
15 | import abc | |
16 | try: |
|
16 | try: | |
17 | from queue import Queue # Py 3 |
|
17 | from queue import Queue # Py 3 | |
18 | except ImportError: |
|
18 | except ImportError: | |
19 | from Queue import Queue # Py 2 |
|
19 | from Queue import Queue # Py 2 | |
20 |
|
20 | |||
21 | # System library imports. |
|
21 | # System library imports. | |
22 | import zmq |
|
22 | import zmq | |
23 |
|
23 | |||
24 | # Local imports. |
|
24 | # Local imports. | |
25 | from IPython.utils.traitlets import HasTraits, Instance, Int |
|
25 | from IPython.utils.traitlets import HasTraits, Instance, Int | |
26 | from IPython.utils.py3compat import with_metaclass |
|
26 | from IPython.utils.py3compat import with_metaclass | |
27 |
|
27 | |||
28 | #----------------------------------------------------------------------------- |
|
28 | #----------------------------------------------------------------------------- | |
29 | # Generic socket interface |
|
29 | # Generic socket interface | |
30 | #----------------------------------------------------------------------------- |
|
30 | #----------------------------------------------------------------------------- | |
31 |
|
31 | |||
32 | class SocketABC(with_metaclass(abc.ABCMeta, object)): |
|
32 | class SocketABC(with_metaclass(abc.ABCMeta, object)): | |
33 | @abc.abstractmethod |
|
33 | @abc.abstractmethod | |
34 | def recv_multipart(self, flags=0, copy=True, track=False): |
|
34 | def recv_multipart(self, flags=0, copy=True, track=False): | |
35 | raise NotImplementedError |
|
35 | raise NotImplementedError | |
36 |
|
36 | |||
37 | @abc.abstractmethod |
|
37 | @abc.abstractmethod | |
38 | def send_multipart(self, msg_parts, flags=0, copy=True, track=False): |
|
38 | def send_multipart(self, msg_parts, flags=0, copy=True, track=False): | |
39 | raise NotImplementedError |
|
39 | raise NotImplementedError | |
40 |
|
40 | |||
41 | SocketABC.register(zmq.Socket) |
|
41 | SocketABC.register(zmq.Socket) | |
42 |
|
42 | |||
43 | #----------------------------------------------------------------------------- |
|
43 | #----------------------------------------------------------------------------- | |
44 | # Dummy socket class |
|
44 | # Dummy socket class | |
45 | #----------------------------------------------------------------------------- |
|
45 | #----------------------------------------------------------------------------- | |
46 |
|
46 | |||
47 | class DummySocket(HasTraits): |
|
47 | class DummySocket(HasTraits): | |
48 | """ A dummy socket implementing (part of) the zmq.Socket interface. """ |
|
48 | """ A dummy socket implementing (part of) the zmq.Socket interface. """ | |
49 |
|
49 | |||
50 | queue = Instance(Queue, ()) |
|
50 | queue = Instance(Queue, ()) | |
51 | message_sent = Int(0) # Should be an Event |
|
51 | message_sent = Int(0) # Should be an Event | |
52 |
|
52 | |||
53 | #------------------------------------------------------------------------- |
|
53 | #------------------------------------------------------------------------- | |
54 | # Socket interface |
|
54 | # Socket interface | |
55 | #------------------------------------------------------------------------- |
|
55 | #------------------------------------------------------------------------- | |
56 |
|
56 | |||
57 | def recv_multipart(self, flags=0, copy=True, track=False): |
|
57 | def recv_multipart(self, flags=0, copy=True, track=False): | |
58 | return self.queue.get_nowait() |
|
58 | return self.queue.get_nowait() | |
59 |
|
59 | |||
60 | def send_multipart(self, msg_parts, flags=0, copy=True, track=False): |
|
60 | def send_multipart(self, msg_parts, flags=0, copy=True, track=False): | |
61 | msg_parts = map(zmq.Message, msg_parts) |
|
61 | msg_parts = list(map(zmq.Message, msg_parts)) | |
62 | self.queue.put_nowait(msg_parts) |
|
62 | self.queue.put_nowait(msg_parts) | |
63 | self.message_sent += 1 |
|
63 | self.message_sent += 1 | |
64 |
|
64 | |||
65 | SocketABC.register(DummySocket) |
|
65 | SocketABC.register(DummySocket) |
General Comments 0
You need to be logged in to leave comments.
Login now