Git Product home page Git Product logo

django-nameko's Introduction

django-nameko

Travis-CI Coverage Status

Branch Build status
master Build Status
develop Build Status

Django wrapper for Nameko microservice framework.

support

tested with

  • python 2.7, 3.5, 3.6, 3.7
  • django 1.11, 2.0, 2.1, 2.2
  • nameko 2.11, 2.12

How to use

from django_nameko import get_pool           

# Within some view or model:
with get_pool().next() as rpc:
    rpc.mailer.send_mail(foo='bar')

Installation

pip install django-nameko

Configuration

# Config to be passed to ClusterRpcProxy 
NAMEKO_CONFIG = { 
    'AMQP_URI': 'amqp://127.0.0.1:5672/'
}  

# Number of proxies to create 
# Each proxy is a single threaded standalone ClusterRpcProxy
NAMEKO_POOL_SIZE = 4
# Set timeout for RPC
NAMEKO_TIMEOUT = 15  # timeout 15 seconds
# Add this dictionary to context_data of every RPC
NAMEKO_CONTEXT_DATA = {
    'hostname': "my.example.com"
}

# Create multiple ClusterRpcProxy pool each one assoiate with a name
# Every pool with pool name different than 'default' will use 'default' pool config as default configuration
NAMEKO_CONFIG={
    'default': {
        'AMQP_URI': 'amqp://',
        'POOL_SIZE': 4,
        'POOL_CONTEXT_DATA': {"common": "multi"},
        'POOL_TIMEOUT': None
    },
    'pool1': {
        'AMQP_URI': 'amqp://pool2',
        'POOL_CONTEXT_DATA': {"name": "pool1", "data": 123},
    },
    'pool2': {
        'AMQP_URI': 'amqp://pool3',
        'POOL_CONTEXT_DATA': {"name": "pool2", "data": 321},
        'POOL_TIMEOUT': 60
    },
    'pool3': {
        'POOL_SIZE': 8,
        'POOL_TIMEOUT': 60
    }
}
# Use multi pool by putting pool name in get_pool(..)
from django_nameko import get_pool

with get_pool('pool1').next() as rpc:
    rpc.mailer.send_mail(foo='bar')
    
# call get_pool() without argument will return the 'default' pool
# but you can override the rpc context data before call, example below.
# it will auto revert back to POOL_CONTEXT_DATA when exit the with block
with get_pool().next() as rpc:
    rpc._worker_ctx.data['SMTP_SECRET'] = 'SECRETXXX'
    rpc.mailer.send_mail(foo='bar')

# try to call rpc outside of with statement block will raise an AttributeError exception 
rpc.mailer.send_mail(bar='foo')
#   File "/usr/local/lib/python2.7/site-packages/django_nameko/rpc.py", line 69, in __getattr__
#     raise AttributeError(item)
# AttributeError: mailer

# New feature (from 0.7.0):
# To dispatch event to any service event listener, for example you have this nameko service:
from nameko.events import event_handler
class EchoService(object):
    name = 'echo'

    @event_handler("echo", "ping")
    def handle_event(self, payload):
        print("service echo received:%s", payload)
# You can sent an event signal to all service listener like this 
from django_nameko import dispatch
dispatch("echo", "ping", {"payload": {"data": 0}})

contribute

to run the tests:

  1. run a local rabbitmq
  2. execute tox
docker run --rm -p 15672:15672 -p 5672:5672 -p 5671:5671 --name nameko-rabbitmq nameko/nameko-rabbitmq:3.6.6
# open another shell then run
python setup.py test
# to run full test with coverage use
tox 

Credits

Thanks to guys who made an awesome Nameko framework.

Maintainers:

django-nameko's People

Contributors

0xgosu avatar and3rson avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

django-nameko's Issues

Solution for multiple rabbitmq servers

Hi, what do you say about following usage allowing us to connect Django app to services behind multiple RabbitMQ servers with different addresses? I have this case and suggested solution would help. Let me know if you like it and I'll send you PR. Thanks

# Settings
NAMEKO_CONFIGS = {
    'default': {
        'config': {
            'AMQP_URL': 'amqp://127.0.0.1:5672/'
        },
        'pool_size': 4
    }
    'other': {
        'config': {
            'AMQP_URL': '...'
        },
        'pool_size': 2
    }
}

# Usage:
# if no argument is passed to get_pool default is used 
# should also backward compatible with old NAMEKO_CONFIG
with get_pool('other').next() as rpc:
    rpc.mailer.send_mail(foo='bar')

Event Listener

Hi, thanks for the wrapper for django and for the update to django 2+

Is there any event_listener option for this wrapper? I couldn't find it in the README.

Requirements missing Django 2.0+

Just installed django-nameko and it uninstalled my django 2.2 and installed 1.11.

I guess it's a requirements missing adjustment.

Is this active?

Is this an active repo? master seems to be broken, pull requests are on hold, and the doc is outdated

python3 compatiblity.

                            for key in self._rpc._worker_ctx.data.keys():
                                del self._rpc._worker_ctx.data[key]

cloud not run in python3

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.