Koed00 / django-q

A multiprocessing distributed task queue for Django
https://django-q.readthedocs.org
MIT License
1.83k stars 289 forks source link

Django-q with django-configurations fails #638

Open jnoortheen opened 2 years ago

jnoortheen commented 2 years ago

python manage.py qcluster fails with following traceback

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/opt/homebrew/Cellar/python@3.9/3.9.9/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/spawn.py", line 116, in spawn_main
    exitcode = _main(fd, parent_sentinel)
  File "/opt/homebrew/Cellar/python@3.9/3.9.9/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/spawn.py", line 126, in _main
    self = reduction.pickle.load(from_parent)
  File ".../lib/python3.9/site-packages/django_q/cluster.py", line 21, in <module>
    apps.check_apps_ready()
  File ".../lib/python3.9/site-packages/django/apps/registry.py", line 135, in check_apps_ready
    settings.INSTALLED_APPS
  File ".../lib/python3.9/site-packages/django/conf/__init__.py", line 82, in __getattr__
    self._setup(name)
  File ".../lib/python3.9/site-packages/django/conf/__init__.py", line 69, in _setup
    self._wrapped = Settings(settings_module)
  File ".../lib/python3.9/site-packages/django/conf/__init__.py", line 170, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/opt/homebrew/Cellar/python@3.9/3.9.9/Frameworks/Python.framework/Versions/3.9/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/Users/noor/src/upwork/plate-recogniser/parkpow/parkpow/settings.py", line 11, in <module>
    class Base(Configuration):
  File ".../lib/python3.9/site-packages/configurations/base.py", line 27, in __new__
    raise ImproperlyConfigured(install_failure)
django.core.exceptions.ImproperlyConfigured: django-configurations settings importer wasn't correctly installed. Please use one of the starter functions to install it as mentioned in the docs: https://django-configurations.readthedocs.io/

But other management tasks run without any issue.

I am using

django-q                          1.3.9
django-configurations             2.3.1
jnoortheen commented 2 years ago

The following change fixed it django_q/cluster.py:19 's content to except Exception: to catch all errors


try:
    apps.check_apps_ready()
except Exception:

    from configurations import importer
    importer.install()
    import django

    django.setup()

I am not sure where to report this. Can django-q include this if configurations is installed ? or django-configurations should be supporting this use-case ?

Also this happens on OSX , I think it is due to the different use of forking the process in linux and darwin

jnoortheen commented 2 years ago

Another workaround is to create a custom django command as

import multiprocessing

multiprocessing.set_start_method("fork", force=True)

from django_q.management.commands import qcluster

class Command(qcluster.Command):
    """on OSX run workers forked"""

    pass