Giter Club home page Giter Club logo

django-celery's Introduction

Django-Celery

APScheduler and Django Celery Setup


APScheduler

>>> from apscheduler.scheduler import Scheduler
>>> sc=Scheduler()
>>> sc.start()
>>> def job_function():
...   print 'hello'
... 
>>> sc.add_cron_job(job_function,month='7',day='24',hour='10',minute=50)

CELERY DOCS

install RabbitMQ

sudo apt-get install rabbitmq-server

When the command completes the broker is already running in the background, ready to move messages for you: Starting rabbitmq-server: SUCCESS.

start and stop rabbitmq

sudo invoke-rc.d rabbitmq-server start and sudo invoke-rc.d rabbitmq-server stop

install django-celery

pip install django-celery

edit setting.py

import djcelery

djcelery.setup_loader()

BROKER_URL = 'amqp://guest:guest@localhost:5672/'

add it to installed_apps ie 'djcelery',

create a new app:

django-admin.py startapp tt

add it to installed_apps

add tasks.py file to app tt

import celery
@celery.task()
def add(x, y):
    return x + y

import datetime
@celery.decorators.periodic_task(run_every=datetime.timedelta(seconds=5))
def my_task():
    # Insert fun-stuff here
    x=wells().save()
    print ('good 1minutes passed.')
    return 0

for example

from datetime import datetime,timedelta
from celery.decorators import periodic_task
@periodic_task(run_every=timedelta(hours=24))
def my_task():
    print "Hello World"

to work periodic_task you must run python manage.py celery beat or python manage.py celeryd -B -l DEBUG

to work task you must run python manage.py celery worker --loglevel=info

testing

>>> from tt import tasks
#always a shortcut to .apply_async
>>> tasks.add.delay(3,4)
#executes 10 seconds from now.
>>> tasks.add.apply_async((3,4),countdown=10)
#executes 10 seconds from now, specifed using eta
>>> tasks.add.apply_async(eta=now + timedelta(seconds=10))

Running the worker as a daemon

install init.d script: celeryd

config file (celeryd) in folder /etc/default

CELERYD_NODES="w1"
# or we could have three nodes:
#CELERYD_NODES="w1 w2 w3"

# Where to chdir at start.
CELERYD_CHDIR="/home/suhail/Desktop/projects/celeryeg2"


# Python interpreter from environment.
ENV_PYTHON="/home/suhail/Envs/celeryenv/bin/python"

# How to call "manage.py celeryd_multi"
CELERYD_MULTI="$ENV_PYTHON $CELERYD_CHDIR/manage.py celeryd_multi"

# How to call "manage.py celeryctl"
CELERYCTL="$ENV_PYTHON $CELERYD_CHDIR/manage.py celeryctl"

# Extra arguments to celeryd
CELERYD_OPTS="--beat --time-limit=300 --concurrency=8"

# %n will be replaced with the nodename.
CELERYD_LOG_FILE="$CELERYD_CHDIR/%n.log"
CELERYD_PID_FILE="$CELERYD_CHDIR/%n.pid"

# Workers should run as an unprivileged user.
#CELERYD_USER="celery"
#CELERYD_GROUP="celery"

# Name of the projects settings module.
export DJANGO_SETTINGS_MODULE="celeryeg2.settings"

django-celery's People

Contributors

ekarthikkumar 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.