I had to perform multiple function calls simultaneously by doing
gevent.spawn(recv)
So if in one recv(spawn), one function is waiting for response,other recv spawn thread started but making it blocking in execution for the second function
def recv(*args, **kwargs):
while 1:
try:
return client_socket_fd.recv((args[0]))
except Exception, e:
err = e.args[0]
if err == errno.EAGAIN or err == errno.EWOULDBLOCK:
sleep(1)
print 'No data available'
continue
else:
break
How can we proceed this?
The coroutine (gevent uses coroutines not threads), will 'yield' to another coroutine on various calls. This includes calling the gevent version of functions such as sleep
, recv
.
In order to call the normal socket/IO/timing code and have gevent work correctly, you need to monkey_patch this code. This means calling a gevent function which replaces built-in Python modules with its own versions. Typically, you want to run
from gevent import monkey; monkey.patch_all()
at the very start of your code. This avoids weird bugs where some code has already started to use the vanilla socket or file code, and then the code changes before the operations have completed.