在此示例中,客户端应立即收到响应,因为查询已生成且根本未使用。 “print msg”行显示在服务器端,但客户端在查询完成执行之前不会收到响应。怎么了?
#!/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])
答案 0 :(得分:4)
ultramysql是一个很好的软件包,但完全没有文档很糟糕。
ultramysql不会自动与gevent兼容。添加到顶部:
来自gevent import monkey的monkey.patch_socket()
问题解决了。