Reputation: 1113
In this example, the client should receive a response immediately, as the query is spawned and not used at all. The "print msg" line shows immediately on the server side, but the client doesn't receive the response until the query has finished executing. What's wrong?
#!/usr/bin/python
import sys
import gevent
from gevent_zeromq import zmq
import umysql
context = zmq.Context()
def serve():
socket = context.socket(zmq.REP)
socket.connect('inproc://backend')
msg = socket.recv()
gevent.spawn(serve)
db = umysql.Connection()
db.connect('localhost',3306,'user','password','database')
gevent.spawn(db.query,"SELECT SLEEP(10)")
socket.send(msg)
socket.close()
print msg
gevent.spawn(serve)
frontend = context.socket(zmq.ROUTER)
frontend.bind('tcp://*:5571')
backend = context.socket(zmq.DEALER)
backend.bind('inproc://backend')
def zeromq_relay(a, b):
while True:
msg = a.recv()
more = a.getsockopt(zmq.RCVMORE)
if more:
b.send(msg, zmq.SNDMORE)
else:
b.send(msg)
a = gevent.spawn(zeromq_relay, frontend, backend)
b = gevent.spawn(zeromq_relay, backend, frontend)
gevent.joinall([a,b])
Upvotes: 0
Views: 1694
Reputation: 1113
ultramysql is a nice package, but the complete lack of docs stinks.
ultramysql is not automatically compatible with gevent. Add to the top:
from gevent import monkey monkey.patch_socket()
problem solved.
Upvotes: 4