0
在此示例中,客戶端應該立即收到響應,因爲查詢已生成且根本未使用。在服務器端立即顯示「print msg」行,但在查詢完成執行之前客戶端不會收到響應。怎麼了?python gevent,zeromq和ultramysql - 大腦傷害
#!/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])
請修正您的代碼示例中的縮進。 –
@eggyal或者'socket = context.socket(zmq.REP)'在'serve()'的主體中,或者它應該在'def'之前。 –
@BurhanKhalid:不錯的地方。 – eggyal