I've looked around a bit but I can't seem to solve this problem I have. I'd like to execute a python script within a view of my django app. I've placed the code I'd like to execute inside a django management command so it can be accessed via command line
python manage.py command-name
. I then tried to run this command usingsubprocess.Popen("python manage.py command-name",shell=True)
.However, this command could take some time to execute so I'd like the view to continue and allow the script to execute in the background. Using subprocess.Popen alone seems to cause the view to hang until the script has finished, so I tried using a thread (following another SA question):
class SubprocessThread(threading.Thread): def __init__(self, c): self.command = c self.stdout = None self.stderr = None threading.Thread.__init__(self) def run(self): p = subprocess.Popen(self.command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) self.stdout, self.stderr = p.communicate()
and then executing it:
t = SubprocessThread("python manage.py command-name") t.setDaemon(True) t.start() t.join()
However, the view still hangs: the cursor has a busy symbol and the AJAX on the page does not load. Otherwise the page's html seems to load fine and commands in the view after the thread call appear to finish normally (before the script finishes). Can someone please help me? I'd like the script to execute and do its own thing without holding up the view or AJAX calls on the page.
score:3
Maybe you should use celery
Celery is a task queue/job queue based on distributed message passing. It is focused on real-time operation
score:2
I wasted a lot of time trying to implement something similar, but had the same problems as you. Eventually, I gave up and implemented a beanstalk queue to handle the work.
http://kr.github.com/beanstalkd/
I put an id on the queue in the Django view, and then have a management command to run the consumer (watched by supervisord).
Using a queue means you could expand to multiple consumers, and allows to better manage the load (pausing the consumer if necessary without losing the work required).
Credit To: stackoverflow.com
Related Query
- Executing a python script using subprocess.Popen() in a django view
- Executing Python Script in Django View [Upload CSV -> Batch Geocoding -> Show Link to Download by script generated CSV]
- OSError: [Errno 2] No such file or directory while using python subprocess in Django
- Python script for Django app to access models without using manage.py shell
- Using Django models in external python script
- Passing data to Django view using Python requests
- Using django ORM from non django python script
- How to call a django view function from external python script
- requests.post from python script to my Django website hosted using Apache giving 403 Forbidden
- Executing command line script from Django & Python
- Apparent memory leak in Python script using Django
- Run Python script in background on remote server with Django view
- How to use Python script in Django using AJAX or something like that?
- Using AJAX to execute Python Django Script
- Remotely accessing sqlite3 in Django using a python script
- Create Django Model Instance using POST method in another Python Script
- Python: Django: How to run django app using a python script
- How to run python script in html using django and get variable from html input form
- Django - how to add data using conditional statement in a separate python script
- Call a python script using only javascript or django
- Using multiprocessing in python script with Django models
- Parsing JSON output using Mechanize and Python Django View
- Creating a JSON response using Django and Python
- How to execute a Python script from the Django shell?
- Django Rest Framework - Could not resolve URL for hyperlinked relationship using view name "user-detail"
- Running a Python script outside of Django
- Oauth for Google API example using Python / Django
- How to get an app name using python in django
- fastest way to create JSON to reflect a tree structure in Python / Django using mptt
- Using python multiprocessing Pool in the terminal and in code modules for Django or Flask
More Query from same tag
- Django Filter timestamp data GROUP BY day, week, month, year
- New to Django. Tried looking at tutorials on how to load css files and not working
- How to create a customized filter search function in Django?
- Django 1.7 multisite User model
- Using ETag for optimistic locking in a Django REST application
- Django Constraining Foreign Keys for multiple nested objects
- Django: can not create multiple model objects with one-to-one relation with one common model
- Django and Gunicorn not working properly through systemd
- Syntax error while saving postgresql server info in setting.py Django
- How to add html id to django's built in log in form
- Caught TypeError while rendering: Decimal('51.8') is not JSON serializable
- How to create referrer url in Django view
- Functional tests in Django reusable apps
- Combining constant and variable value for template default value
- Unable to set file permissions in S3 using boto and django