GithubHelp home page GithubHelp logo

gabrielfarah / django-rq-rest Goto Github PK

View Code? Open in Web Editor NEW
1.0 3.0 1.0 26 KB

Build slim and easy async rest taks using djangorestframework and django-rq

License: GNU General Public License v3.0

Python 100.00%
django-rq django-rest-framework asynchronous-tasks python-rq

django-rq-rest's Introduction

django-rq-rest

This library helps you build slim and easy async rest taks using django-rq and django-rest-framework. This library has views to be used in your Django app and code to help you setup the clients.

One big difference between this library and a typical django-rq setup is that your tasks are completely decoupled from the main app, this makes them easier to develop and update both independently.

Usage

1. Create a new async rest view

First, we create a new viw inside our views.py file.

from django_rq_rest.views import AsyncView

class ImageClassifierView(AsyncView):
    """
    This view receives a base 64 encoded image inside 
    the payload with key "b64_image" and the worker returns a 
    classification label.
    """
    renderer_classes = (JSONRenderer,)
    permission_classes = (IsAuthenticated,)

    job_file = 'jobs'
    queue_name = settings.ML_QUEUE # settings.py defined queue 
    job_name = 'image_face_recognition'
    job_params = ['b64_image']
    view_name = 'image-recognition'

We add it to our urls.py like any other view.

urlpatterns = [
    ...
    url(r'^face-classifier/$', 
    ImageClassifierView.as_view(), name=ImageClassifierView.view_name)
    ...

In this example we selected that:

  1. We will have a jobs.py file in our worker.
  2. Inside our jobs.py file there will be a function called image_face_recognition.
  3. The function image_face_recognition will have one parameter called b64_image.
  4. This function will perform the task and return the result when polled.
2. Creating the taks

Lets create and define jobs.py as our worker task.

import base64
import json
import awesome_custom_ml_lib

def image_face_recognition(b64_image):
    ... your code here ...
    return json.dumps(classified_img)

In this example what matters is that we have followed the conventions defined in the ImageClassifierView view.

3. Creating the worker

To create the worker that listen to Redis, we create a file (in my case worker.py) and we add the following:

from django_rq_rest.worker.base import BaseWorker
worker = BaseWorker('ml_queue', redis_url='localhost:6379/1')
worker.work()

once we run python worker.py we should start being able to run our service.

4. Using the endpoint
import requests

req = requests.post('localhost:8080/face-classifier', json={'b64_image':'Base64 img...'})
data = req.json()
poll = None
while not poll:
    poll = requests.get(data['url']).json().get('result')
....
Do something ...

django-rq-rest's People

Contributors

gabrielfarah avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Forkers

inactivist

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.