[Fixed]-Run python script from django

1👍

OK it seems you want to run a script outside of the HTTP request/response cycle,
I’d recommend you make a Django admin command, because a script will need to e.g. have access to the database environment to update records

from django.core.management.base import BaseCommand
from yourapp.models import Thing
import your_script

class Command(BaseCommand):
    help = 'Closes the specified poll for voting'

    def handle(self, *args, **options):
        # Do your processing here
        your_script.process(Thing.objects.all())

With this you can call ./manage.py process_thing and it’ll run independently

More on Django admin commands here, https://docs.djangoproject.com/en/1.8/howto/custom-management-commands/

If the processing is triggered programmatically e.g. from user requests,
you’ll have to setup a queue and have a task job created for each request, I’d try Celery, more on that here http://celery.readthedocs.org/en/latest/django/first-steps-with-django.html

👤bakkal

Leave a comment