How to run background application via Django

66 views
Skip to first unread message

Sugita Shinsuke

unread,
Jan 8, 2015, 5:09:52 AM1/8/15
to django...@googlegroups.com
Hi there.

I'd like to run background application.

I coded this code.

I wrote the urls.py and I added in the view.

def run_junix(request):
    cmd = "cd app_path;nohup python background_app.py &"
    import subprocess
    proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

    return HttpResponse("bg test")

Is it good way? or using supervisor or, if you know the better way, would you tell me?

Anyone who know this matter, please help.

Mario Gudelj

unread,
Jan 8, 2015, 5:19:20 AM1/8/15
to django...@googlegroups.com

Look into celery. It's the best and easiest way to run bg jobs imo

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/0a8222ff-a22c-44a5-a098-2b663aaca294%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Sugita Shinsuke

unread,
Jan 8, 2015, 10:55:09 PM1/8/15
to django...@googlegroups.com
Hi somecallitblues 

Thank you for replying.
I installed Celery 3.1.17.

And, I setup 

I created add method in tasks.py

my tasks.py
----
from __future__ import absolute_import

from celery import shared_task


@shared_task
def add(x, y):
    return x + y
----

I also called my add method from my view.

hoge_view.py
---
def run_celery(request):
    result = add.delay(3, 8)
    while not result.ready():
        print 'spam'
    print result.get()
    return HttpResponse("test")
---

I called it from url.

"error: timed out" was occurred.

Why is it timed out? Could you help me?


2015年1月8日木曜日 19時19分20秒 UTC+9 somecallitblues:

Vijay Khemlani

unread,
Jan 8, 2015, 11:24:25 PM1/8/15
to django...@googlegroups.com
Have you done the celery tutorial?

Async tasks require you to have a broker (rabbitmq or something) and also to create some workers that actually execute the task.

Sugita Shinsuke

unread,
Jan 8, 2015, 11:49:15 PM1/8/15
to django...@googlegroups.com
Hi Vijay Khemlani

Thank you for replying. 

>Have you done the celery tutorial?
I have not done the celery tutorial yet.

But, I ran Redis and set below in my settings.py

BROKER_URL = 'redis://localhost'
CELERY_RESULT_BACKEND = 'redis'
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_ACCEPT_CONTENT = ['json']

I also ran celery command.

celery -A app_name worker -l info 

Is it the tutorial URL you said?


Thank you.

2015年1月9日金曜日 13時24分25秒 UTC+9 Vijay Khemlani:

Sugita Shinsuke

unread,
Jan 9, 2015, 1:40:36 AM1/9/15
to django...@googlegroups.com
Hi Vijay Khemlani

I tried tutorial. and "error: timed out" was disappeared!

http://celery.readthedocs.org/en/latest/django/first-steps-with-django.html
was deprecated.

http://docs.celeryproject.org/en/master/django/first-steps-with-django.html
is new.

I added the code in my app/__init__.py:

---
from __future__ import absolute_import

# This will make sure the app is always imported when
# Django starts so that shared_task will use this app.
from .celery import app as celery_app
---

How do I call other process?

Is it okey?

@shared_task
def my_task():
    cmd = "cd program_path;python my_program.py"
    import subprocess
    proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    return proc


I call my my_task function in the view

---
def run_celery(request):
    result = my_task.delay()
    while not result.ready():
        print 'processing...'
    print result.get()

    return HttpResponse("test")


2015年1月9日金曜日 13時49分15秒 UTC+9 Sugita Shinsuke:
Reply all
Reply to author
Forward
0 new messages