import os
from celery.loaders.djangoapp import Loader as DjangoLoader
from celery.loaders.default import Loader as DefaultLoader
from django.conf import settings
from django.core.management import setup_environ

"""
.. class:: Loader

The current loader class.

"""
Loader = DefaultLoader
if settings.configured:
    Loader = DjangoLoader
else:
    if not callable(getattr(os, "fork", None)):
        # Platform doesn't support fork()
        # XXX On systems without fork, multiprocessing seems to be launching
        # the processes in some other way which does not copy the memory
        # of the parent process. This means that any configured env might
        # be lost. This is a hack to make it work on Windows.
        # A better way might be to use os.environ to set the currently
        # used configuration method so to propogate it to the "child"
        # processes. But this has to be experimented with.
        # [asksol/heyman]
        try:
            settings_mod = os.environ.get("DJANGO_SETTINGS_MODULE",
                                          "settings")
            project_settings = __import__(settings_mod, {}, {}, [''])
            setup_environ(project_settings)
            Loader = DjangoLoader
        except ImportError:
            pass

"""
.. data:: current_loader

The current loader instance.

"""
current_loader = Loader()


"""
.. data:: settings

The global settings object.

"""
settings = current_loader.conf

import os
from celery.loaders.djangoapp import Loader as DjangoLoader
from celery.loaders.default import Loader as DefaultLoader
from django.conf import settings
from django.core.management import setup_environ

"""
.. class:: Loader

The current loader class.

"""
Loader = DefaultLoader
if settings.configured:
    Loader = DjangoLoader
else:
    try:
        # A settings module may be defined, but Django didn't attempt to 
        # load it yet. As an alternative to calling the private _setup(),
        # we could also check whether DJANGO_SETTINGS_MODULE is set.
        settings._setup() 
    except ImportError:
        if not callable(getattr(os, "fork", None)):
            # Platform doesn't support fork()
            # XXX On systems without fork, multiprocessing seems to be launching
            # the processes in some other way which does not copy the memory
            # of the parent process. This means that any configured env might
            # be lost. This is a hack to make it work on Windows.
            # A better way might be to use os.environ to set the currently
            # used configuration method so to propogate it to the "child"
            # processes. But this has to be experimented with.
            # [asksol/heyman]
            try:
                settings_mod = os.environ.get("DJANGO_SETTINGS_MODULE",
                                              "settings")
                project_settings = __import__(settings_mod, {}, {}, [''])
                setup_environ(project_settings)
                Loader = DjangoLoader
            except ImportError:
                pass
    else:
        Loader = DjangoLoader

"""
.. data:: current_loader

The current loader instance.

"""
current_loader = Loader()


"""
.. data:: settings

The global settings object.

"""
settings = current_loader.conf

Use a django settings module, if defined, even if it wasn't already loaded by Django (for example, when using ./celeryd directly rather than the celeryd management command.
Use a django settings module, if defined, even if it wasn't already loaded by Django (for example, when using ./celeryd directly rather than the celeryd management command.

  import os
  from celery.loaders.djangoapp import Loader as DjangoLoader
  from celery.loaders.default import Loader as DefaultLoader
  from django.conf import settings
  from django.core.management import setup_environ
  
  """
  .. class:: Loader
  
  The current loader class.
  
  """
  Loader = DefaultLoader
  if settings.configured:
      Loader = DjangoLoader
  else:
+     try:
+         # A settings module may be defined, but Django didn't attempt to 
+         # load it yet. As an alternative to calling the private _setup(),
+         # we could also check whether DJANGO_SETTINGS_MODULE is set.
+         settings._setup() 
+     except ImportError:
-     if not callable(getattr(os, "fork", None)):
+         if not callable(getattr(os, "fork", None)):
-         # Platform doesn't support fork()
+             # Platform doesn't support fork()
-         # XXX On systems without fork, multiprocessing seems to be launching
+             # XXX On systems without fork, multiprocessing seems to be launching
-         # the processes in some other way which does not copy the memory
+             # the processes in some other way which does not copy the memory
-         # of the parent process. This means that any configured env might
+             # of the parent process. This means that any configured env might
-         # be lost. This is a hack to make it work on Windows.
+             # be lost. This is a hack to make it work on Windows.
-         # A better way might be to use os.environ to set the currently
+             # A better way might be to use os.environ to set the currently
-         # used configuration method so to propogate it to the "child"
+             # used configuration method so to propogate it to the "child"
-         # processes. But this has to be experimented with.
+             # processes. But this has to be experimented with.
-         # [asksol/heyman]
+             # [asksol/heyman]
-         try:
+             try:
-             settings_mod = os.environ.get("DJANGO_SETTINGS_MODULE",
+                 settings_mod = os.environ.get("DJANGO_SETTINGS_MODULE",
-                                           "settings")
+                                               "settings")
-             project_settings = __import__(settings_mod, {}, {}, [''])
+                 project_settings = __import__(settings_mod, {}, {}, [''])
-             setup_environ(project_settings)
+                 setup_environ(project_settings)
-             Loader = DjangoLoader
+                 Loader = DjangoLoader
-         except ImportError:
+             except ImportError:
-             pass
+                 pass
+     else:
+         Loader = DjangoLoader
  
  """
  .. data:: current_loader
  
  The current loader instance.
  
  """
  current_loader = Loader()
  
  
  """
  .. data:: settings
  
  The global settings object.
  
  """
  settings = current_loader.conf
  
Use a django settings module, if defined, even if it wasn't already loaded by Django (for example, when using ./celeryd directly rather than the celeryd management command.
## Code Before:
import os
from celery.loaders.djangoapp import Loader as DjangoLoader
from celery.loaders.default import Loader as DefaultLoader
from django.conf import settings
from django.core.management import setup_environ

"""
.. class:: Loader

The current loader class.

"""
Loader = DefaultLoader
if settings.configured:
    Loader = DjangoLoader
else:
    if not callable(getattr(os, "fork", None)):
        # Platform doesn't support fork()
        # XXX On systems without fork, multiprocessing seems to be launching
        # the processes in some other way which does not copy the memory
        # of the parent process. This means that any configured env might
        # be lost. This is a hack to make it work on Windows.
        # A better way might be to use os.environ to set the currently
        # used configuration method so to propogate it to the "child"
        # processes. But this has to be experimented with.
        # [asksol/heyman]
        try:
            settings_mod = os.environ.get("DJANGO_SETTINGS_MODULE",
                                          "settings")
            project_settings = __import__(settings_mod, {}, {}, [''])
            setup_environ(project_settings)
            Loader = DjangoLoader
        except ImportError:
            pass

"""
.. data:: current_loader

The current loader instance.

"""
current_loader = Loader()


"""
.. data:: settings

The global settings object.

"""
settings = current_loader.conf

## Instruction:
Use a django settings module, if defined, even if it wasn't already loaded by Django (for example, when using ./celeryd directly rather than the celeryd management command.
## Code After:
import os
from celery.loaders.djangoapp import Loader as DjangoLoader
from celery.loaders.default import Loader as DefaultLoader
from django.conf import settings
from django.core.management import setup_environ

"""
.. class:: Loader

The current loader class.

"""
Loader = DefaultLoader
if settings.configured:
    Loader = DjangoLoader
else:
    try:
        # A settings module may be defined, but Django didn't attempt to 
        # load it yet. As an alternative to calling the private _setup(),
        # we could also check whether DJANGO_SETTINGS_MODULE is set.
        settings._setup() 
    except ImportError:
        if not callable(getattr(os, "fork", None)):
            # Platform doesn't support fork()
            # XXX On systems without fork, multiprocessing seems to be launching
            # the processes in some other way which does not copy the memory
            # of the parent process. This means that any configured env might
            # be lost. This is a hack to make it work on Windows.
            # A better way might be to use os.environ to set the currently
            # used configuration method so to propogate it to the "child"
            # processes. But this has to be experimented with.
            # [asksol/heyman]
            try:
                settings_mod = os.environ.get("DJANGO_SETTINGS_MODULE",
                                              "settings")
                project_settings = __import__(settings_mod, {}, {}, [''])
                setup_environ(project_settings)
                Loader = DjangoLoader
            except ImportError:
                pass
    else:
        Loader = DjangoLoader

"""
.. data:: current_loader

The current loader instance.

"""
current_loader = Loader()


"""
.. data:: settings

The global settings object.

"""
settings = current_loader.conf
