[zeromq-dev] pyzmq and django

Dan Fairs dan.fairs at gmail.com
Tue May 8 17:48:24 CEST 2012


> I want to build an app that consume some zmq message and put them in django model, django app has others functions.
> 
> I don't how can i mix zmq ioloop and django loop.
> 
> Is there an easy solution ?
> 

I do this. Just write a Django management command which never finishes. Here's some code off the top of my head, not tested at all:

import json
import zmq
from django.core.management.base import NoArgsCommand
from django import transaction
from myapp.models import SomeModel

class Command(NoArgsCommand):

  def handle_noargs(self, **options):
    c = zmq.Context()
    s = c.socket(zmq.PULL)
    s.connect('tcp://127.0.0,1:5000')
    try:
      while True:
        self.process_message(s.recv())
    finally:
      s.close()
      c.term()

  @transaction.commit_on_success
  def process_message(self, message):
    m = json.loads(message)
    SomeModel.objects.create(title=m['title'])


That would live in (for example) myapp/management/commands.

Cheers,
Dan

--
Dan Fairs | dan.fairs at gmail.com | www.fezconsulting.com

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20120508/4db3c585/attachment.htm>


More information about the zeromq-dev mailing list