Skip to content

lahim/django-zmq

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Django-ZMQ

Simple integration ZMQ with Django.

Build Status

Quick start

  1. Add "django-zmq" to your INSTALLED_APPS setting like this:
INSTALLED_APPS = [
...
'djangozmq',
]
  1. Add ZMQ settings into the settings.py file, example below:
ZMQ_PULL_SOCKET = 'tcp://127.0.0.1:5000'
ZMQ_PUSH_SOCKET = 'tcp://127.0.0.1:5001'
ZMQ_TASKS = [
    'exampleapp.tasks.send_text_message',
]
  1. Run ZMQ master using below django command:
./manage.py zmqrunmaster
  1. Run ZMQ slave using below django command:
./manage.py zmqrunslave
  1. Now, you can send data to the socket as presented below:
import zmq 
 
ctx = zmq.Context() 
task_socket = ctx.socket(zmq.PUSH) 
task_socket.connect('tcp://127.0.0.1:5000')                                                                                                                                                                

task_socket.send_json({ 
        'task': 'exampleapp.tasks.send_text_message', 
        'kwargs': {'a': 1, 'b': 2, 'c': 3, 'd': 4}, 
    }) 

Example:

Below you can find a tasks definition: exampleapp/tasks.py:

def send_text_message(*args, **kwargs):
    print('Sending text message...')
    # add your code here for sending text message...
    print('Text message was sent.')

Here is an example of calling a task from your django app:

from djangozmq.zmq.sockets import SocketManager                                                                                                                                                            

sm = SocketManager()                                                                                                                                                                                       
sm.call_task('exampleapp.tasks.send_text_message', {'foo': 'bar'})                                                                                                                                     

Periodic tasks

  1. For periodic tasks you need to run django-zmq beat using below command:
./manage.py zmqrunbeat
  1. Add periodic tasks into the settings.py - example below:
...
ZMQ_PERIODIC_TASKS = [
    ('exampleapp.tasks.check_all_not_completed_jobs', None, 10),
]

ZMQ_PERIODIC_TASKS is a list of tuple where:

  • 1st element is a task name,
  • 2nd element is a task kwargs,
  • 3rd element is a interval - it's an int value which represents seconds. Interval value means run this task every X-seconds.

About

Simple integration ZMQ with Django

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages