Giter Site home page Giter Site logo

django-event-queue's Introduction

Event Queue

Requirements

  1. Using cache (memcache, redis,... )
  2. Python 3

Installation

Run pip install django-event-queue

Add settings

# settings.py

INSTALLED_APPS = [
    ...,
    'event_queue',
]

# example cache settings
CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
        'LOCATION': '127.0.0.1:11211',
        'TIMEOUT': 300,
    }
}

Default configuration for AMQP connection

{
    'host': os.environ.get('AMQP_HOST', 'localhost'),
    'port': os.environ.get('AMQP_HOST', 5672),
    'vhost': os.environ.get('AMQP_VHOST', '/'),
    'username': os.environ.get('AMQP_USERNAME', 'guest'),
    'password': os.environ.get('AMQP_PASSWORD', 'guest'),
}

If new configuration likes:

{
    'username': os.environ.get('AMQP_USERNAME', 'username'),
    'password': os.environ.get('AMQP_PASSWORD', 'password'),
}

then final configuration used for make connection is:

{
    'host': os.environ.get('AMQP_HOST', 'localhost'),
    'port': os.environ.get('AMQP_HOST', 5672),
    'vhost': os.environ.get('AMQP_VHOST', '/'),
    'username': os.environ.get('AMQP_USERNAME', 'username'),
    'password': os.environ.get('AMQP_PASSWORD', 'password'),
}

Run migrate data

python manage.py migrate

Example

Long Polling

Exmaple of a custom command.

# management/commands/long_poll.py
from django.core.management import BaseCommand

from event_queue.long_poll import LongPoll


class Command(BaseCommand):
    help = 'Event Queue Long Polling'

    def add_arguments(self, parser):
        pass

    def handle(self, *args, **options):
        long_poll = LongPoll(exchange=None, username='rabbitmq', password='rabbitmq', vhost='localhost',
                             queue_name='long_poll')
        long_poll()

Event processing

class PrintBody(QueueProcessFacade):
    TASK_NAME = 'PrintBody'

    def process(self, event):
        """
        Main process for event should be overrode in subclass
        :param event:
        :return:
        """
        if self.is_closed(event):
            return False
        print(event.payload)
        return True

django-event-queue's People

Contributors

minhgdd-teko avatar ducminhgd avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.