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 using subprocess.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.
The subprocess. popen() is one of the most useful methods which is used to create a process. This process can be used to run a command or execute binary. The process creation is also called as spawning a new process which is different from the current process.
Popen . The main difference is that subprocess. run executes a command and waits for it to finish, while with subprocess. Popen you can continue doing your stuff while the process finishes and then just repeatedly call subprocess.
exception subprocess. CalledProcessError. Subclass of SubprocessError , raised when a process run by check_call() , check_output() , or run() (with check=True ) returns a non-zero exit status.
Maybe you should use celery
Celery is a task queue/job queue based on distributed message passing. It is focused on real-time operation
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With