Forums

How to Keep python manage.py qcluster Running Consistently?

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!

Hi there, our consoles are not designed for long-running processes. This is exactly what always-on tasks are for.

As for why it doesn't show up in your list of processes: where are you running the code to check this?

Thank you for your response. I ran the code to check the running processes in the directory where Django is running, using IPython. As shown in the screenshot ( https://ibb.co/zXkJ3Wn ), qcluster is indeed running, but I can't seem to capture it using this method. Could you please guide me on how to resolve this issue?

Additionally, while checking in the consoles, even though I've only issued the command once, I noticed that qcluster seems to be running 5 times. Is this normal, or could this be part of the issue?

As an update, I was able to confirm that the process is running using ps aux | grep qcluster. What should I do next?

Looking at the qcluster documentation leads me to think that it will not work on PythonAnywhere.

I made a mistake in my previous post where I mentioned that we can verify the qcluster process using the command ps aux | grep qcluster.

I've been able to confirm that the following code successfully runs qcluster from Python:

import os
from django.core.management import call_command

# Set the Django settings module
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings')

# Initialize Django
import django
django.setup()

# Invoke the qcluster command
call_command('qcluster')

So, if I can just verify the qcluster process from Python, I can automate the entire process. For some reason, I'm unable to detect the qcluster process using the ps command. If anyone is knowledgeable about this, please enlighten me.

There is no shared process namespace between web apps, consoles, tasks etc on PythonAnywhere.

jmindview2, I don't know the details of your use case, but in my case, I only needed django-q running for specific user-initiated processes. I handle this by spawning a django-q process and killing it when I'm done. Take a look at my code; it may help: https://www.pythonanywhere.com/forums/topic/33482/

@calvinc460 We would welcome PR based on your experience to https://github.com/pythonanywhere/help_pages/blob/master/articles/AsyncInWebApps.md