Skip to content

Commit 4d2cdca

Browse files
ozamiatintemoto
authored andcommitted
green.zmq: Support {send,recv}_{string,json,pyobj} wrappers
The following wrappers added: * send_string/recv_string * send_json/recv_json * send_pyobj/recv_pyobj This change is also needed to make these methods thread-safe. eventlet#343
1 parent ac4e28f commit 4d2cdca

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

eventlet/green/zmq.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,12 @@ def wrapper(dest_fn):
194194
_Socket_send = _Socket.send
195195
_Socket_send_multipart = _Socket.send_multipart
196196
_Socket_recv_multipart = _Socket.recv_multipart
197+
_Socket_send_string = _Socket.send_string
198+
_Socket_recv_string = _Socket.recv_string
199+
_Socket_send_pyobj = _Socket.send_pyobj
200+
_Socket_recv_pyobj = _Socket.recv_pyobj
201+
_Socket_send_json = _Socket.send_json
202+
_Socket_recv_json = _Socket.recv_json
197203
_Socket_getsockopt = _Socket.getsockopt
198204

199205

@@ -313,6 +319,48 @@ def send_multipart(self, msg_parts, flags=0, copy=True, track=False):
313319
with self._eventlet_send_lock:
314320
return _Socket_send_multipart(self, msg_parts, flags, copy, track)
315321

322+
@_wraps(_Socket.send_string)
323+
def send_string(self, msg_parts, flags=0, copy=True, track=False):
324+
"""A send_string method that's safe to use when multiple
325+
greenthreads are calling send, send_string, recv and
326+
recv_string on the same socket.
327+
"""
328+
if flags & NOBLOCK:
329+
return _Socket_send_string(self, msg_parts, flags, copy, track)
330+
331+
# acquire lock here so the subsequent calls to send for the
332+
# message parts after the first don't block
333+
with self._eventlet_send_lock:
334+
return _Socket_send_string(self, msg_parts, flags, copy, track)
335+
336+
@_wraps(_Socket.send_pyobj)
337+
def send_pyobj(self, msg_parts, flags=0, copy=True, track=False):
338+
"""A send_pyobj method that's safe to use when multiple
339+
greenthreads are calling send, send_pyobj, recv and
340+
recv_pyobj on the same socket.
341+
"""
342+
if flags & NOBLOCK:
343+
return _Socket_send_pyobj(self, msg_parts, flags, copy, track)
344+
345+
# acquire lock here so the subsequent calls to send for the
346+
# message parts after the first don't block
347+
with self._eventlet_send_lock:
348+
return _Socket_send_pyobj(self, msg_parts, flags, copy, track)
349+
350+
@_wraps(_Socket.send_json)
351+
def send_json(self, msg_parts, flags=0, copy=True, track=False):
352+
"""A send_json method that's safe to use when multiple
353+
greenthreads are calling send, send_json, recv and
354+
recv_json on the same socket.
355+
"""
356+
if flags & NOBLOCK:
357+
return _Socket_send_json(self, msg_parts, flags, copy, track)
358+
359+
# acquire lock here so the subsequent calls to send for the
360+
# message parts after the first don't block
361+
with self._eventlet_send_lock:
362+
return _Socket_send_json(self, msg_parts, flags, copy, track)
363+
316364
@_wraps(_Socket.recv)
317365
def recv(self, flags=0, copy=True, track=False):
318366
"""A recv method that's safe to use when multiple greenthreads
@@ -357,3 +405,45 @@ def recv_multipart(self, flags=0, copy=True, track=False):
357405
# message parts after the first don't block
358406
with self._eventlet_recv_lock:
359407
return _Socket_recv_multipart(self, flags, copy, track)
408+
409+
@_wraps(_Socket.recv_string)
410+
def recv_string(self, flags=0, copy=True, track=False):
411+
"""A recv_string method that's safe to use when multiple
412+
greenthreads are calling send, send_string, recv and
413+
recv_string on the same socket.
414+
"""
415+
if flags & NOBLOCK:
416+
return _Socket_recv_string(self, flags, copy, track)
417+
418+
# acquire lock here so the subsequent calls to recv for the
419+
# message parts after the first don't block
420+
with self._eventlet_recv_lock:
421+
return _Socket_recv_string(self, flags, copy, track)
422+
423+
@_wraps(_Socket.recv_pyobj)
424+
def recv_pyobj(self, flags=0, copy=True, track=False):
425+
"""A recv_pyobj method that's safe to use when multiple
426+
greenthreads are calling send, send_pyobj, recv and
427+
recv_pyobj on the same socket.
428+
"""
429+
if flags & NOBLOCK:
430+
return _Socket_recv_pyobj(self, flags, copy, track)
431+
432+
# acquire lock here so the subsequent calls to recv for the
433+
# message parts after the first don't block
434+
with self._eventlet_recv_lock:
435+
return _Socket_recv_pyobj(self, flags, copy, track)
436+
437+
@_wraps(_Socket.recv_json)
438+
def recv_json(self, flags=0, copy=True, track=False):
439+
"""A recv_json method that's safe to use when multiple
440+
greenthreads are calling send, send_json, recv and
441+
recv_json on the same socket.
442+
"""
443+
if flags & NOBLOCK:
444+
return _Socket_recv_json(self, flags, copy, track)
445+
446+
# acquire lock here so the subsequent calls to recv for the
447+
# message parts after the first don't block
448+
with self._eventlet_recv_lock:
449+
return _Socket_recv_json(self, flags, copy, track)

0 commit comments

Comments
 (0)