4

我需要在 django 和 celery 中使用 redis 作为代理来实现 zmq 发布者。但我的客户没有收到任何东西。

任务.py

  from celery import task
  import zmq


  try:
      context = zmq.Context()
      publisher = context.socket(zmq.PUB)
      publisher.bind('tcp://192.168.0.14:9997')
  except:
      pass

  def app_delivery():
      publisher.send("testid : I am a task ")
      return "Passed"
  @task.task(ignore_result=True)
  def add():
      print "I am going to push message "
      return app_delivery()

视图.py

from django.http import HttpResponse
from notification_core.tasks import add

def resp(request):
    param = request.GET.get('param')
    param = int(param)
    i=0
    while i<param:
        add.delay()
        i+=1
    return HttpResponse("Done")

然后使用命令运行工人——

python manage_back.py celery worker --loglevel=info

工人控制台输出

[2013-04-20 10:37:06,400: WARNING/PoolWorker-2] I am going to push message
[2013-04-20 10:37:06,400: INFO/MainProcess] Task notification_core.tasks.add[6d44bb48-799e-43dc-af3e-278c0db732c4] succeeded in 0.000511169433594s: 'Passed'
[2013-04-20 10:37:06,575: INFO/MainProcess] Got task from broker: notification_core.tasks.add[8a787b31-5375-4c05-9ddc-2fc66b54ee19]
[2013-04-20 10:37:06,575: WARNING/PoolWorker-3] I am going to push message
[2013-04-20 10:37:06,576: INFO/MainProcess] Task notification_core.tasks.add[8a787b31-5375-4c05-9ddc-2fc66b54ee19] succeeded in 0.000941038131714s: 'Passed'
[2013-04-20 10:37:06,751: INFO/MainProcess] Got task from broker: notification_core.tasks.add[d73ebcdf-9765-43f6-823a-ad03881afa50]
[2013-04-20 10:37:06,752: WARNING/PoolWorker-4] I am going to push message
[2013-04-20 10:37:06,752: INFO/MainProcess] Task notification_core.tasks.add[d73ebcdf-9765-43f6-823a-ad03881afa50] succeeded in 0.000416040420532s: 'Passed'

zmq_client 文件

import zmq
context = zmq.Context()

subscriber = context.socket(zmq.SUB)
subscriber.connect('tcp://192.168.0.14:9997')
subscriber.setsockopt(zmq.SUBSCRIBE, "testid")
while True:
    message = subscriber.recv()
    print "=========="
    print message
    print "=========="  

我在客户端没有得到任何输出,需要一些建议。

4

1 回答 1

0

你是先启动你的客户吗?通常,您希望在 PUB 之前启动 SUB。此外,您可能会发现使用信封很有帮助,尽管在这里看起来有点矫枉过正。

http://zguide.zeromq.org/page%3aall#Pub-Sub-Message-Envelopes

如果您不只是像前面提到的那样简单地吃一个相关的例外,希望这能给我们一些帮助。

于 2013-06-12T18:19:09.260 回答