socket.py
66 lines
| 2.2 KiB
| text/x-python
|
PythonLexer
epatters
|
r8411 | """ Defines a dummy socket implementing (part of) the zmq.Socket interface. """ | ||
#----------------------------------------------------------------------------- | ||||
# Copyright (C) 2012 The IPython Development Team | ||||
# | ||||
# Distributed under the terms of the BSD License. The full license is in | ||||
# the file COPYING, distributed as part of this software. | ||||
#----------------------------------------------------------------------------- | ||||
#----------------------------------------------------------------------------- | ||||
# Imports | ||||
#----------------------------------------------------------------------------- | ||||
# Standard library imports. | ||||
epatters
|
r8418 | import abc | ||
Thomas Kluyver
|
r13354 | try: | ||
from queue import Queue # Py 3 | ||||
except ImportError: | ||||
from Queue import Queue # Py 2 | ||||
epatters
|
r8411 | |||
# System library imports. | ||||
import zmq | ||||
# Local imports. | ||||
from IPython.utils.traitlets import HasTraits, Instance, Int | ||||
#----------------------------------------------------------------------------- | ||||
epatters
|
r8418 | # Generic socket interface | ||
#----------------------------------------------------------------------------- | ||||
class SocketABC(object): | ||||
__metaclass__ = abc.ABCMeta | ||||
@abc.abstractmethod | ||||
def recv_multipart(self, flags=0, copy=True, track=False): | ||||
raise NotImplementedError | ||||
@abc.abstractmethod | ||||
def send_multipart(self, msg_parts, flags=0, copy=True, track=False): | ||||
raise NotImplementedError | ||||
SocketABC.register(zmq.Socket) | ||||
#----------------------------------------------------------------------------- | ||||
epatters
|
r8411 | # Dummy socket class | ||
#----------------------------------------------------------------------------- | ||||
class DummySocket(HasTraits): | ||||
""" A dummy socket implementing (part of) the zmq.Socket interface. """ | ||||
Thomas Kluyver
|
r13354 | queue = Instance(Queue, ()) | ||
epatters
|
r8411 | message_sent = Int(0) # Should be an Event | ||
#------------------------------------------------------------------------- | ||||
epatters
|
r8418 | # Socket interface | ||
epatters
|
r8411 | #------------------------------------------------------------------------- | ||
def recv_multipart(self, flags=0, copy=True, track=False): | ||||
return self.queue.get_nowait() | ||||
def send_multipart(self, msg_parts, flags=0, copy=True, track=False): | ||||
msg_parts = map(zmq.Message, msg_parts) | ||||
self.queue.put_nowait(msg_parts) | ||||
self.message_sent += 1 | ||||
epatters
|
r8418 | |||
SocketABC.register(DummySocket) | ||||