您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

466 行
18KB

  1. # coding: utf-8
  2. """The :mod:`zmq` module wraps the :class:`Socket` and :class:`Context`
  3. found in :mod:`pyzmq <zmq>` to be non blocking.
  4. """
  5. __zmq__ = __import__('zmq')
  6. import eventlet.hubs
  7. from eventlet.patcher import slurp_properties
  8. from eventlet.support import greenlets as greenlet
  9. __patched__ = ['Context', 'Socket']
  10. slurp_properties(__zmq__, globals(), ignore=__patched__)
  11. from collections import deque
  12. try:
  13. # alias XREQ/XREP to DEALER/ROUTER if available
  14. if not hasattr(__zmq__, 'XREQ'):
  15. XREQ = DEALER
  16. if not hasattr(__zmq__, 'XREP'):
  17. XREP = ROUTER
  18. except NameError:
  19. pass
  20. class LockReleaseError(Exception):
  21. pass
  22. class _QueueLock(object):
  23. """A Lock that can be acquired by at most one thread. Any other
  24. thread calling acquire will be blocked in a queue. When release
  25. is called, the threads are awoken in the order they blocked,
  26. one at a time. This lock can be required recursively by the same
  27. thread."""
  28. def __init__(self):
  29. self._waiters = deque()
  30. self._count = 0
  31. self._holder = None
  32. self._hub = eventlet.hubs.get_hub()
  33. def __nonzero__(self):
  34. return bool(self._count)
  35. __bool__ = __nonzero__
  36. def __enter__(self):
  37. self.acquire()
  38. def __exit__(self, type, value, traceback):
  39. self.release()
  40. def acquire(self):
  41. current = greenlet.getcurrent()
  42. if (self._waiters or self._count > 0) and self._holder is not current:
  43. # block until lock is free
  44. self._waiters.append(current)
  45. self._hub.switch()
  46. w = self._waiters.popleft()
  47. assert w is current, 'Waiting threads woken out of order'
  48. assert self._count == 0, 'After waking a thread, the lock must be unacquired'
  49. self._holder = current
  50. self._count += 1
  51. def release(self):
  52. if self._count <= 0:
  53. raise LockReleaseError("Cannot release unacquired lock")
  54. self._count -= 1
  55. if self._count == 0:
  56. self._holder = None
  57. if self._waiters:
  58. # wake next
  59. self._hub.schedule_call_global(0, self._waiters[0].switch)
  60. class _BlockedThread(object):
  61. """Is either empty, or represents a single blocked thread that
  62. blocked itself by calling the block() method. The thread can be
  63. awoken by calling wake(). Wake() can be called multiple times and
  64. all but the first call will have no effect."""
  65. def __init__(self):
  66. self._blocked_thread = None
  67. self._wakeupper = None
  68. self._hub = eventlet.hubs.get_hub()
  69. def __nonzero__(self):
  70. return self._blocked_thread is not None
  71. __bool__ = __nonzero__
  72. def block(self, deadline=None):
  73. if self._blocked_thread is not None:
  74. raise Exception("Cannot block more than one thread on one BlockedThread")
  75. self._blocked_thread = greenlet.getcurrent()
  76. if deadline is not None:
  77. self._hub.schedule_call_local(deadline - self._hub.clock(), self.wake)
  78. try:
  79. self._hub.switch()
  80. finally:
  81. self._blocked_thread = None
  82. # cleanup the wakeup task
  83. if self._wakeupper is not None:
  84. # Important to cancel the wakeup task so it doesn't
  85. # spuriously wake this greenthread later on.
  86. self._wakeupper.cancel()
  87. self._wakeupper = None
  88. def wake(self):
  89. """Schedules the blocked thread to be awoken and return
  90. True. If wake has already been called or if there is no
  91. blocked thread, then this call has no effect and returns
  92. False."""
  93. if self._blocked_thread is not None and self._wakeupper is None:
  94. self._wakeupper = self._hub.schedule_call_global(0, self._blocked_thread.switch)
  95. return True
  96. return False
  97. class Context(__zmq__.Context):
  98. """Subclass of :class:`zmq.Context`
  99. """
  100. def socket(self, socket_type):
  101. """Overridden method to ensure that the green version of socket is used
  102. Behaves the same as :meth:`zmq.Context.socket`, but ensures
  103. that a :class:`Socket` with all of its send and recv methods set to be
  104. non-blocking is returned
  105. """
  106. if self.closed:
  107. raise ZMQError(ENOTSUP)
  108. return Socket(self, socket_type)
  109. def _wraps(source_fn):
  110. """A decorator that copies the __name__ and __doc__ from the given
  111. function
  112. """
  113. def wrapper(dest_fn):
  114. dest_fn.__name__ = source_fn.__name__
  115. dest_fn.__doc__ = source_fn.__doc__
  116. return dest_fn
  117. return wrapper
  118. # Implementation notes: Each socket in 0mq contains a pipe that the
  119. # background IO threads use to communicate with the socket. These
  120. # events are important because they tell the socket when it is able to
  121. # send and when it has messages waiting to be received. The read end
  122. # of the events pipe is the same FD that getsockopt(zmq.FD) returns.
  123. #
  124. # Events are read from the socket's event pipe only on the thread that
  125. # the 0mq context is associated with, which is the native thread the
  126. # greenthreads are running on, and the only operations that cause the
  127. # events to be read and processed are send(), recv() and
  128. # getsockopt(zmq.EVENTS). This means that after doing any of these
  129. # three operations, the ability of the socket to send or receive a
  130. # message without blocking may have changed, but after the events are
  131. # read the FD is no longer readable so the hub may not signal our
  132. # listener.
  133. #
  134. # If we understand that after calling send() a message might be ready
  135. # to be received and that after calling recv() a message might be able
  136. # to be sent, what should we do next? There are two approaches:
  137. #
  138. # 1. Always wake the other thread if there is one waiting. This
  139. # wakeup may be spurious because the socket might not actually be
  140. # ready for a send() or recv(). However, if a thread is in a
  141. # tight-loop successfully calling send() or recv() then the wakeups
  142. # are naturally batched and there's very little cost added to each
  143. # send/recv call.
  144. #
  145. # or
  146. #
  147. # 2. Call getsockopt(zmq.EVENTS) and explicitly check if the other
  148. # thread should be woken up. This avoids spurious wake-ups but may
  149. # add overhead because getsockopt will cause all events to be
  150. # processed, whereas send and recv throttle processing
  151. # events. Admittedly, all of the events will need to be processed
  152. # eventually, but it is likely faster to batch the processing.
  153. #
  154. # Which approach is better? I have no idea.
  155. #
  156. # TODO:
  157. # - Support MessageTrackers and make MessageTracker.wait green
  158. _Socket = __zmq__.Socket
  159. _Socket_recv = _Socket.recv
  160. _Socket_send = _Socket.send
  161. _Socket_send_multipart = _Socket.send_multipart
  162. _Socket_recv_multipart = _Socket.recv_multipart
  163. _Socket_send_string = _Socket.send_string
  164. _Socket_recv_string = _Socket.recv_string
  165. _Socket_send_pyobj = _Socket.send_pyobj
  166. _Socket_recv_pyobj = _Socket.recv_pyobj
  167. _Socket_send_json = _Socket.send_json
  168. _Socket_recv_json = _Socket.recv_json
  169. _Socket_getsockopt = _Socket.getsockopt
  170. class Socket(_Socket):
  171. """Green version of :class:`zmq.core.socket.Socket
  172. The following three methods are always overridden:
  173. * send
  174. * recv
  175. * getsockopt
  176. To ensure that the ``zmq.NOBLOCK`` flag is set and that sending or receiving
  177. is deferred to the hub (using :func:`eventlet.hubs.trampoline`) if a
  178. ``zmq.EAGAIN`` (retry) error is raised
  179. For some socket types, the following methods are also overridden:
  180. * send_multipart
  181. * recv_multipart
  182. """
  183. def __init__(self, context, socket_type):
  184. super(Socket, self).__init__(context, socket_type)
  185. self.__dict__['_eventlet_send_event'] = _BlockedThread()
  186. self.__dict__['_eventlet_recv_event'] = _BlockedThread()
  187. self.__dict__['_eventlet_send_lock'] = _QueueLock()
  188. self.__dict__['_eventlet_recv_lock'] = _QueueLock()
  189. def event(fd):
  190. # Some events arrived at the zmq socket. This may mean
  191. # there's a message that can be read or there's space for
  192. # a message to be written.
  193. send_wake = self._eventlet_send_event.wake()
  194. recv_wake = self._eventlet_recv_event.wake()
  195. if not send_wake and not recv_wake:
  196. # if no waiting send or recv thread was woken up, then
  197. # force the zmq socket's events to be processed to
  198. # avoid repeated wakeups
  199. _Socket_getsockopt(self, EVENTS)
  200. hub = eventlet.hubs.get_hub()
  201. self.__dict__['_eventlet_listener'] = hub.add(hub.READ,
  202. self.getsockopt(FD),
  203. event,
  204. lambda _: None,
  205. lambda: None)
  206. self.__dict__['_eventlet_clock'] = hub.clock
  207. @_wraps(_Socket.close)
  208. def close(self, linger=None):
  209. super(Socket, self).close(linger)
  210. if self._eventlet_listener is not None:
  211. eventlet.hubs.get_hub().remove(self._eventlet_listener)
  212. self.__dict__['_eventlet_listener'] = None
  213. # wake any blocked threads
  214. self._eventlet_send_event.wake()
  215. self._eventlet_recv_event.wake()
  216. @_wraps(_Socket.getsockopt)
  217. def getsockopt(self, option):
  218. result = _Socket_getsockopt(self, option)
  219. if option == EVENTS:
  220. # Getting the events causes the zmq socket to process
  221. # events which may mean a msg can be sent or received. If
  222. # there is a greenthread blocked and waiting for events,
  223. # it will miss the edge-triggered read event, so wake it
  224. # up.
  225. if (result & POLLOUT):
  226. self._eventlet_send_event.wake()
  227. if (result & POLLIN):
  228. self._eventlet_recv_event.wake()
  229. return result
  230. @_wraps(_Socket.send)
  231. def send(self, msg, flags=0, copy=True, track=False):
  232. """A send method that's safe to use when multiple greenthreads
  233. are calling send, send_multipart, recv and recv_multipart on
  234. the same socket.
  235. """
  236. if flags & NOBLOCK:
  237. result = _Socket_send(self, msg, flags, copy, track)
  238. # Instead of calling both wake methods, could call
  239. # self.getsockopt(EVENTS) which would trigger wakeups if
  240. # needed.
  241. self._eventlet_send_event.wake()
  242. self._eventlet_recv_event.wake()
  243. return result
  244. # TODO: pyzmq will copy the message buffer and create Message
  245. # objects under some circumstances. We could do that work here
  246. # once to avoid doing it every time the send is retried.
  247. flags |= NOBLOCK
  248. with self._eventlet_send_lock:
  249. while True:
  250. try:
  251. return _Socket_send(self, msg, flags, copy, track)
  252. except ZMQError as e:
  253. if e.errno == EAGAIN:
  254. self._eventlet_send_event.block()
  255. else:
  256. raise
  257. finally:
  258. # The call to send processes 0mq events and may
  259. # make the socket ready to recv. Wake the next
  260. # receiver. (Could check EVENTS for POLLIN here)
  261. self._eventlet_recv_event.wake()
  262. @_wraps(_Socket.send_multipart)
  263. def send_multipart(self, msg_parts, flags=0, copy=True, track=False):
  264. """A send_multipart method that's safe to use when multiple
  265. greenthreads are calling send, send_multipart, recv and
  266. recv_multipart on the same socket.
  267. """
  268. if flags & NOBLOCK:
  269. return _Socket_send_multipart(self, msg_parts, flags, copy, track)
  270. # acquire lock here so the subsequent calls to send for the
  271. # message parts after the first don't block
  272. with self._eventlet_send_lock:
  273. return _Socket_send_multipart(self, msg_parts, flags, copy, track)
  274. @_wraps(_Socket.send_string)
  275. def send_string(self, u, flags=0, copy=True, encoding='utf-8'):
  276. """A send_string method that's safe to use when multiple
  277. greenthreads are calling send, send_string, recv and
  278. recv_string on the same socket.
  279. """
  280. if flags & NOBLOCK:
  281. return _Socket_send_string(self, u, flags, copy, encoding)
  282. # acquire lock here so the subsequent calls to send for the
  283. # message parts after the first don't block
  284. with self._eventlet_send_lock:
  285. return _Socket_send_string(self, u, flags, copy, encoding)
  286. @_wraps(_Socket.send_pyobj)
  287. def send_pyobj(self, obj, flags=0, protocol=2):
  288. """A send_pyobj method that's safe to use when multiple
  289. greenthreads are calling send, send_pyobj, recv and
  290. recv_pyobj on the same socket.
  291. """
  292. if flags & NOBLOCK:
  293. return _Socket_send_pyobj(self, obj, flags, protocol)
  294. # acquire lock here so the subsequent calls to send for the
  295. # message parts after the first don't block
  296. with self._eventlet_send_lock:
  297. return _Socket_send_pyobj(self, obj, flags, protocol)
  298. @_wraps(_Socket.send_json)
  299. def send_json(self, obj, flags=0, **kwargs):
  300. """A send_json method that's safe to use when multiple
  301. greenthreads are calling send, send_json, recv and
  302. recv_json on the same socket.
  303. """
  304. if flags & NOBLOCK:
  305. return _Socket_send_json(self, obj, flags, **kwargs)
  306. # acquire lock here so the subsequent calls to send for the
  307. # message parts after the first don't block
  308. with self._eventlet_send_lock:
  309. return _Socket_send_json(self, obj, flags, **kwargs)
  310. @_wraps(_Socket.recv)
  311. def recv(self, flags=0, copy=True, track=False):
  312. """A recv method that's safe to use when multiple greenthreads
  313. are calling send, send_multipart, recv and recv_multipart on
  314. the same socket.
  315. """
  316. if flags & NOBLOCK:
  317. msg = _Socket_recv(self, flags, copy, track)
  318. # Instead of calling both wake methods, could call
  319. # self.getsockopt(EVENTS) which would trigger wakeups if
  320. # needed.
  321. self._eventlet_send_event.wake()
  322. self._eventlet_recv_event.wake()
  323. return msg
  324. deadline = None
  325. if hasattr(__zmq__, 'RCVTIMEO'):
  326. sock_timeout = self.getsockopt(__zmq__.RCVTIMEO)
  327. if sock_timeout == -1:
  328. pass
  329. elif sock_timeout > 0:
  330. deadline = self._eventlet_clock() + sock_timeout / 1000.0
  331. else:
  332. raise ValueError(sock_timeout)
  333. flags |= NOBLOCK
  334. with self._eventlet_recv_lock:
  335. while True:
  336. try:
  337. return _Socket_recv(self, flags, copy, track)
  338. except ZMQError as e:
  339. if e.errno == EAGAIN:
  340. # zmq in its wisdom decided to reuse EAGAIN for timeouts
  341. if deadline is not None and self._eventlet_clock() > deadline:
  342. e.is_timeout = True
  343. raise
  344. self._eventlet_recv_event.block(deadline=deadline)
  345. else:
  346. raise
  347. finally:
  348. # The call to recv processes 0mq events and may
  349. # make the socket ready to send. Wake the next
  350. # receiver. (Could check EVENTS for POLLOUT here)
  351. self._eventlet_send_event.wake()
  352. @_wraps(_Socket.recv_multipart)
  353. def recv_multipart(self, flags=0, copy=True, track=False):
  354. """A recv_multipart method that's safe to use when multiple
  355. greenthreads are calling send, send_multipart, recv and
  356. recv_multipart on the same socket.
  357. """
  358. if flags & NOBLOCK:
  359. return _Socket_recv_multipart(self, flags, copy, track)
  360. # acquire lock here so the subsequent calls to recv for the
  361. # message parts after the first don't block
  362. with self._eventlet_recv_lock:
  363. return _Socket_recv_multipart(self, flags, copy, track)
  364. @_wraps(_Socket.recv_string)
  365. def recv_string(self, flags=0, encoding='utf-8'):
  366. """A recv_string method that's safe to use when multiple
  367. greenthreads are calling send, send_string, recv and
  368. recv_string on the same socket.
  369. """
  370. if flags & NOBLOCK:
  371. return _Socket_recv_string(self, flags, encoding)
  372. # acquire lock here so the subsequent calls to recv for the
  373. # message parts after the first don't block
  374. with self._eventlet_recv_lock:
  375. return _Socket_recv_string(self, flags, encoding)
  376. @_wraps(_Socket.recv_json)
  377. def recv_json(self, flags=0, **kwargs):
  378. """A recv_json method that's safe to use when multiple
  379. greenthreads are calling send, send_json, recv and
  380. recv_json on the same socket.
  381. """
  382. if flags & NOBLOCK:
  383. return _Socket_recv_json(self, flags, **kwargs)
  384. # acquire lock here so the subsequent calls to recv for the
  385. # message parts after the first don't block
  386. with self._eventlet_recv_lock:
  387. return _Socket_recv_json(self, flags, **kwargs)
  388. @_wraps(_Socket.recv_pyobj)
  389. def recv_pyobj(self, flags=0):
  390. """A recv_pyobj method that's safe to use when multiple
  391. greenthreads are calling send, send_pyobj, recv and
  392. recv_pyobj on the same socket.
  393. """
  394. if flags & NOBLOCK:
  395. return _Socket_recv_pyobj(self, flags)
  396. # acquire lock here so the subsequent calls to recv for the
  397. # message parts after the first don't block
  398. with self._eventlet_recv_lock:
  399. return _Socket_recv_pyobj(self, flags)