From 3f28abcb5836d415d54861a42b55389e49995013 Mon Sep 17 00:00:00 2001 From: Ask Solem Date: Tue, 25 Jan 2011 12:54:00 +0100 Subject: [PATCH] gevent: Make sure synchronous calls are blocking --- kombu/utils/__init__.py | 45 ++++++++++++++++++++++++++++++----------- 1 file changed, 33 insertions(+), 12 deletions(-) diff --git a/kombu/utils/__init__.py b/kombu/utils/__init__.py index 40275cfe..2ec1b4c9 100644 --- a/kombu/utils/__init__.py +++ b/kombu/utils/__init__.py @@ -12,23 +12,44 @@ except ImportError: ctypes = None -blocking = None -try: - from eventlet.patches import is_monkey_patched as is_eventlet - import socket +def __how_to_block(): + ## -eventlet- + try: + from eventlet.patches import is_monkey_patched as is_eventlet + import socket - if is_eventlet(socket): - from eventlet import spawn + if is_eventlet(socket): + from eventlet import spawn - def blocking(fun, *args, **kwargs): - return spawn(fun, *args, **kwargs).wait() -except ImportError: - pass + def __blocking__(fun, *args, **kwargs): + return spawn(fun, *args, **kwargs).wait() -if blocking is None: - def blocking(fun, *args, **kwargs): + return __blocking__ + except ImportError: + pass + + # -gevent- + try: + from gevent import socket as _gsocket + import socket + + if socket.socket is _gsocket.socket: + from gevent import Greenlet + + def __blocking__(fun, *args, **kwargs): + return Greenlet.spawn(fun, *args, **kwargs).get() + + return __blocking__ + except ImportError: + pass + + def __blocking__(fun, *args, **kwargs): return fun(*args, **kwargs) + return __blocking__ + +blocking = __how_to_block() + def say(m, *s): sys.stderr.write(str(m) % s + "\n")