用芹菜(Broker redis)在django中实现zeromq发布者

时间:2013-04-20 05:38:05

标签: python django celery zeromq django-celery

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

tasks.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()

views.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 file

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 "=========="  

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

1 个答案:

答案 0 :(得分:0)

您是否先启动了客户?通常,您希望在PUB之前启动SUB。你也可以发现使用信封很有帮助,虽然看起来有点矫枉过正。

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

如果您不是简单地吃了前面提到的相关例外,那么希望能给我们一些东西。