This post is a continuation of the discussion from this forum thread. https://www.pythonanywhere.com/forums/topic/30810/
I've clarified a part of the issue I was facing. Specifically, when I run the command python manage.py qcluster and then close the console, I later discover that this command stops running. Given this issue with the console command unexpectedly terminating, I'm looking for advice on how best to keep it running consistently. This doesn't seem to be related to PythonAnywhere's Always-on tasks feature, leading me to believe it might be a separate issue entirely.
One potential solution I'm considering involves using Python's subprocess and psutil libraries to check if python manage.py qcluster is running, and if not, to execute the command. Here's a code snippet:
import psutil
import subprocess
def is_qcluster_running():
for process in psutil.process_iter(attrs=['pid', 'cmdline']):
try:
cmdline = process.info['cmdline']
if cmdline:
if 'python' in cmdline and 'manage.py' in cmdline and 'qcluster' in cmdline:
return True
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
pass
return False
if not is_qcluster_running():
subprocess.run(["python", "manage.py", "qcluster"])
Oddly enough, even when python manage.py qcluster is running, it doesn't seem to appear in the list of processes when I run for process in psutil.process_iter(attrs=['pid', 'cmdline']):. For example, the output is as follows:
{'cmdline': ['bash', '--rcfile', '/tmp/tmprc.29623960', '-i'], 'pid': 1}
{'cmdline': ['python', 'manage.py', 'shell'], 'pid': 125}
{'cmdline': ['python', 'manage.py', 'shell'], 'pid': 190}
{'cmdline': [], 'pid': 208}
Any idea why this might be the case? Would registering this program as an Always-on task be a suitable approach? I'm eager to hear any insights, suggestions, or best practices you may have. Thank you in advance!