Skip to main content

Microservice with django

Project description

Tutorial

Install

pip install bgtasks

Configuration

settings.py
AMQP = {
    'USERNAME': 'guest',
    'PASSWORD': 'guest',
    'VHOST': '/',
    'HOST': 'localhost',
    'PORT': 5672,
}

RPC methods

app1 views.py
from django.http import HttpResponse
from bgtasks import RPCClient

def add_user(request):
    data = {
        'username': 'John',
        'password':'secret123'
    }
    client = RPCClient()
    # Create user and get id
    data = client.call('user.add', data)
    html = "<html><body>Create user id %s.</body></html>" % data
    return HttpResponse(html)
app2 tasks.py
from django.contrib.auth import User
from bgtasks import rpc_tasks

@rpc_tasks('user.add')
def add_user(data):
    user = User.objects.create(username=data['username'])
    user.set_password(data['password'])
    return user.id

for listing rabbitmq

python manange.py tasks

Job methods

app1 views.py
from django.http import HttpResponse
from bgtasks import JobClient

def send_email(request):
    data = {
        'email': 'user@exampe.com'
    }
    client = JobClient()
    data = client.call('user.send_mail', data)
    html = "<html><body>Email send</body></html>"
    return HttpResponse(html)
app2 tasks.py
from django.contrib.auth import User
from django.core.mail import send_mail
from bgtasks import job_tasks

@job_tasks('user.send_mail')
def send_mail(data):
    send_mail(
        'Hi!',
        'Hello World.',
        'from@example.com',
        [data['email']],
        fail_silently=False,
    )

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

bgtasks-0.0.1-py3-none-any.whl (8.1 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page