zeroMQ初体验-17.应答模式进阶(三)-定制路由2

XREP-REQ模式:
典型的"老妈模式",只有当她真的要听你说时,她才能听的进去。所以首先,得要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传递数据时,先得确定他已经存在,不然数据可就丢了。

(未完待续)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值