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