The behavior is synchronous because your own code is synchronous. gevent is just a coroutine library using an event loop. This does not magically turn synchronous code into asynchronous code.
Please see the documentation at: http://www.gevent.org/servers.html
They say that on the servers one green appears for each connection (not for each request). Therefore, the execution of multiple queries for the same connection is serialized.
If you want to process several requests at the same time for the same connection, you need to create new green fields or delegate processing to the green pool.
Here is an example (generating a potion for each request):
import gevent from gevent.pywsgi import WSGIServer from gevent.lock import Semaphore from geventwebsocket.handler import WebSocketHandler from datetime import datetime def process(ws,data,sem): print('{} got data "{}"'.format(datetime.now().strftime('%H:%M:%S'), data)) gevent.sleep(5) with sem: ws.send(data) def app(environ, start_response): ws = environ['wsgi.websocket'] sem = Semaphore() while True: data = ws.receive() gevent.spawn(process,ws,data,sem) server = WSGIServer(("", 10004), app,handler_class=WebSocketHandler) server.serve_forever()
Pay attention to the presence of a semaphore. Since processing is parallel, it is necessary to prevent the simultaneous recording of two parallel skeletons on the socket, which leads to message corruption.
The latter, with this implementation, does not guarantee that responses will be sent in the order of requests.
source share