Celery and Django simple example

Assuming you have both Python’s celery and django-celery installed, create the following tasks.py file under your app: utils/tasks.py from celery import task # other imports @task() def create_user(data): user = User.objects.create_user( username=data[‘username’], email=None, password=data[‘password’] ) user.save() profile = UserProfile() profile.user = user profile.token = generate_token() profile.save() return None Delete your utils/utilities.py file in your example … Read more

Run a Scrapy spider in a Celery Task

The twisted reactor cannot be restarted. A work around for this is to let the celery task fork a new child process for each crawl you want to execute as proposed in the following post: Running Scrapy spiders in a Celery task This gets around the “reactor cannot be restart-able” issue by utilizing the multiprocessing … Read more

How are Django channels different than celery?

Channels in Django are meant for asynchronous handling of requests. The standard model Django uses is Request-Response but that has significant limitations. We cannot do anything outside the restrictions of that model. Channels came about to allow Web Socket support and building complex applications around Web Sockets, so that we can send multiple messages, manage … Read more

Running Scrapy spiders in a Celery task

Okay here is how I got Scrapy working with my Django project that uses Celery for queuing up what to crawl. The actual workaround came primarily from joehillen’s code located here http://snippets.scrapy.org/snippets/13/ First the tasks.py file from celery import task @task() def crawl_domain(domain_pk): from crawl import domain_crawl return domain_crawl(domain_pk) Then the crawl.py file from multiprocessing … Read more

How to inspect and cancel Celery tasks by task name

# Retrieve tasks # Reference: http://docs.celeryproject.org/en/latest/reference/celery.events.state.html query = celery.events.state.tasks_by_type(your_task_name) # Kill tasks # Reference: http://docs.celeryproject.org/en/latest/userguide/workers.html#revoking-tasks for uuid, task in query: celery.control.revoke(uuid, terminate=True)