gevent: Make sure synchronous calls are blocking

This commit is contained in:
Ask Solem 2011-01-25 12:54:00 +01:00
parent 4b50b6ba38
commit 3f28abcb58
1 changed files with 33 additions and 12 deletions

View File

@ -12,7 +12,8 @@ except ImportError:
ctypes = None ctypes = None
blocking = None def __how_to_block():
## -eventlet-
try: try:
from eventlet.patches import is_monkey_patched as is_eventlet from eventlet.patches import is_monkey_patched as is_eventlet
import socket import socket
@ -20,15 +21,35 @@ try:
if is_eventlet(socket): if is_eventlet(socket):
from eventlet import spawn from eventlet import spawn
def blocking(fun, *args, **kwargs): def __blocking__(fun, *args, **kwargs):
return spawn(fun, *args, **kwargs).wait() return spawn(fun, *args, **kwargs).wait()
return __blocking__
except ImportError: except ImportError:
pass pass
if blocking is None: # -gevent-
def blocking(fun, *args, **kwargs): 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 fun(*args, **kwargs)
return __blocking__
blocking = __how_to_block()
def say(m, *s): def say(m, *s):
sys.stderr.write(str(m) % s + "\n") sys.stderr.write(str(m) % s + "\n")