-
Notifications
You must be signed in to change notification settings - Fork 56
/
core.py
139 lines (108 loc) · 3.92 KB
/
core.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
"""Asyncio compatibility with zeromq."""
__all__ = ['Socket', 'Context']
import collections
import functools
import pickle
import asyncio
import zmq
class Socket(zmq.Socket):
"""Tulip's version of zmq.Socket"""
_loop = None
_sock_fd = None
_buffer = None
def __init__(self, context, socket_type, *, loop=None):
super().__init__(context, socket_type)
if loop is None:
loop = asyncio.get_event_loop()
self._loop = loop
self._buffer = collections.deque()
self._sock_fd = self.getsockopt(zmq.FD)
@asyncio.coroutine
def recv(self, flags=0, copy=True, track=False):
if flags & zmq.NOBLOCK:
return super().recv(flags, copy, track)
# ensure the zmq.NOBLOCK flag is part of flags
flags |= zmq.NOBLOCK
# Attempt to complete this operation indefinitely
try:
return zmq.Socket.recv(self, flags, copy, track)
except zmq.ZMQError as e:
if e.errno != zmq.EAGAIN:
raise
# defer to the event loop until we're notified the socket is readable
fut = asyncio.Future(loop=self._loop)
self._recv(fut, False, flags, copy, track)
return (yield from fut)
def _recv(self, fut, registered, *args):
if registered:
self._loop.remove_reader(self._sock_fd)
if fut.cancelled():
return
try:
data = super().recv(*args)
except zmq.ZMQError as exc:
if exc.errno != zmq.EAGAIN:
fut.set_exception(exc)
return
self._loop.add_reader(self._sock_fd, self._recv, fut, True, *args)
except Exception as exc:
fut.set_exception(exc)
else:
fut.set_result(data)
def send(self, data, flags=0, copy=True, track=False):
assert isinstance(data, bytes), repr(data)
if not data:
return
fut = asyncio.Future(loop=self._loop)
if not self._buffer:
# if we're given the NOBLOCK flag act as normal
# and let the EAGAIN get raised
if flags & zmq.NOBLOCK:
res = super().send(data, flags, copy, track)
fut.set_result(res)
return fut
# ensure the zmq.NOBLOCK flag is part of flags
flags |= zmq.NOBLOCK
# Attempt to complete this operation indefinitely
try:
res = super().send(data, flags, copy, track)
fut.set_result(res)
return fut
except zmq.ZMQError as exc:
if exc.errno != zmq.EAGAIN:
fut.set_exception(exc)
return
self._loop.add_writer(self._sock_fd, self._send_ready)
self._buffer.append((fut, data, flags, copy, track))
return fut
def _send_ready(self):
while self._buffer:
entry = self._buffer.popleft()
fut, *args = entry
try:
res = super().send(*args)
fut.set_result(res)
except zmq.ZMQError as exc:
if exc.errno != zmq.EAGAIN:
fut.set_exception(exc)
else:
self._buffer.appendleft(entry)
return
except Exception as exc:
fut.set_exception(exc)
return
self._loop.remove_writer(self._sock_fd)
def recv_pyobj(self, flags=0):
s = yield from self.recv(flags)
return pickle.loads(s)
class Context(zmq.Context):
"""Replacement for `zmq.Context`.
Creates special version of Socket object."""
_loop = None
_socket_class = None
def __init__(self, io_threads=1, *, loop=None):
super().__init__(io_threads)
if loop is None:
loop = asyncio.get_event_loop()
self._loop = loop
self._socket_class = functools.partial(Socket, loop=loop)