XREP-REQ模式:
典型的"老妈模式",只有当她真的要听你说时,她才能听的进去。所以首先,得要REQ告诉你“她准备好了,你可以讲了”,然后,你才能倾吐...
一般来说与XREQ一样,一个REQ只能连接一个XREP(除非你想做容错,不过,不建议那样)。
实例模型:
[img]http://github.com/imatix/zguide/raw/master/images/fig35.png[/img]
传递的数据结构:
[img]http://github.com/imatix/zguide/raw/master/images/fig36.png[/img]
注意点:
如果"老妈"没有和你主动联系,那么就不要向她发一个字!
XREP-REP模式:
这种模式并不属于经典的应用范畴,通常的做法是XREP-XREQ-REP,由“分销商”来负责数据的传递。不过既然有这两种类型,不妨试着联通看看~
实例模型:
[img]http://github.com/imatix/zguide/raw/master/images/fig37.png[/img]
数据结构:
[img]http://github.com/imatix/zguide/raw/master/images/fig38.png[/img]
注意:
因为REP不像REQ那样,他是被动的,所以在往REP传递数据时,先得确定他已经存在,不然数据可就丢了。
(未完待续)
典型的"老妈模式",只有当她真的要听你说时,她才能听的进去。所以首先,得要REQ告诉你“她准备好了,你可以讲了”,然后,你才能倾吐...
一般来说与XREQ一样,一个REQ只能连接一个XREP(除非你想做容错,不过,不建议那样)。
实例模型:
[img]http://github.com/imatix/zguide/raw/master/images/fig35.png[/img]
import time
import random
from threading import Thread
import zmq
import zhelpers
NBR_WORKERS = 10
def worker_thread(context):
worker = context.socket(zmq.REQ)
# We use a string identity for ease here
zhelpers.set_id(worker)
worker.connect("ipc://routing.ipc")
total = 0
while True:
# Tell the router we're ready for work
worker.send("ready")
# Get workload from router, until finished
workload = worker.recv()
finished = workload == "END"
if finished:
print "Processed: %d tasks" % total
break
total += 1
# Do some random work
time.sleep(random.random() / 10 + 10 ** -9)
context = zmq.Context()
client = context.socket(zmq.XREP)
client.bind("ipc://routing.ipc")
for _ in xrange(NBR_WORKERS):
Thread(target=worker_thread, args=(context,)).start()
for _ in xrange(NBR_WORKERS * 10):
# LRU worker is next waiting in the queue
address = client.recv()
empty = client.recv()
ready = client.recv()
client.send(address, zmq.SNDMORE)
client.send("", zmq.SNDMORE)
client.send("This is the workload")
# Now ask mama to shut down and report their results
for _ in xrange(NBR_WORKERS):
address = client.recv()
empty = client.recv()
ready = client.recv()
client.send(address, zmq.SNDMORE)
client.send("", zmq.SNDMORE)
client.send("END")
time.sleep(1) # Give 0MQ/2.0.x time to flush output
传递的数据结构:
[img]http://github.com/imatix/zguide/raw/master/images/fig36.png[/img]
注意点:
如果"老妈"没有和你主动联系,那么就不要向她发一个字!
XREP-REP模式:
这种模式并不属于经典的应用范畴,通常的做法是XREP-XREQ-REP,由“分销商”来负责数据的传递。不过既然有这两种类型,不妨试着联通看看~
实例模型:
[img]http://github.com/imatix/zguide/raw/master/images/fig37.png[/img]
import time
import zmq
import zhelpers
context = zmq.Context()
client = context.socket(zmq.XREP)
client.bind("ipc://routing.ipc")
worker = context.socket(zmq.REP)
worker.setsockopt(zmq.IDENTITY, "A")
worker.connect("ipc://routing.ipc")
# Wait for sockets to stabilize
time.sleep(1)
client.send("A", zmq.SNDMORE)
client.send("address 3", zmq.SNDMORE)
client.send("address 2", zmq.SNDMORE)
client.send("address 1", zmq.SNDMORE)
client.send("", zmq.SNDMORE)
client.send("This is the workload")
# Worker should get just the workload
zhelpers.dump(worker)
# We don't play with envelopes in the worker
worker.send("This is the reply")
# Now dump what we got off the XREP socket…
zhelpers.dump(client)
数据结构:
[img]http://github.com/imatix/zguide/raw/master/images/fig38.png[/img]
注意:
因为REP不像REQ那样,他是被动的,所以在往REP传递数据时,先得确定他已经存在,不然数据可就丢了。
(未完待续)