I implemented a simple app with event callback facility, but the server is not able to serve the requests at the same time. It waits for the first req to complete before serving second. Not sure what I am doing wrong. I am using this as reference: http://bottlepy.org/docs/dev/async.html
import time
from gevent import monkey; monkey.patch_all()
import bottle
from gevent import Greenlet
from gevent import pywsgi
from gevent import queue
import gevent
def worker(body):
print 'worker called'
data = [ 'one', 'two', 'three', 'four' ]
for d in data:
body.put(d)
gevent.sleep(5)
body.put(StopIteration)
@bottle.route('/')
def def1():
body = gevent.queue.Queue()
g = Greenlet.spawn(worker, body)
return body
def main():
bottle.run(host='0',port=8081, server="gevent")
if __name__ == '__main__':
main()
Thanks
Your code is working exactly as it should. I think you're confused about what
server="gevent"
does.If you didn't specify
server="gevent"
, then when you ran your server, and made two requests to your server at the same time, one would hang until the other completed. By "two requests to your server" I mean hit it twice at the same time, either from your browser or from curl or from wget.With
server="gevent"
, if you hit your server from two clients at the same time, then both requests will be serviced concurrently.WSGI is inherently single-threaded, which is why you're seeing this behaviour in the non-gevent case.
Now, you seem to also be trying to defer some work during each request, by putting into a gevent queue. This is separate from
server="gevent"
, and has nothing to do with Bottle or WSGI.You're also explicitly telling gevent to sleep for 5 seconds, and it happily obliges. So each request to your server will take
5 seconds * len(data)
.Hope that makes sense. Good luck!