Parcourir la source

Django: Use config_from_object(string) so that the object does not have to be serialized (Issue #1603)

Ask Solem il y a 11 ans
Parent
commit
d40b2d2bcb

+ 6 - 2
docs/django/first-steps-with-django.rst

@@ -39,7 +39,7 @@ that defines the Celery instance:
     from django.conf import settings
 
     app = Celery('proj.celery')
-    app.config_from_object(settings)
+    app.config_from_object('django.conf:settings')
     app.autodiscover_tasks(settings.INSTALLED_APPS, related_name='tasks')
 
     @celery.task(bind=True)
@@ -58,9 +58,13 @@ for Celery.  This means that you don't have to use multiple
 configuration files, and instead configure Celery directly
 from the Django settings.
 
+You can pass the object directly here, but using a string is better since
+then the worker doesn't have to serialize the object when using Windows
+or execv:
+
 .. code-block:: python
 
-    app.config_from_object(settings)
+    app.config_from_object('django.conf:settings')
 
 Next, a common practice for reusable apps is to define all tasks
 in a separate ``tasks.py`` module, and Celery does have a way to

+ 2 - 2
docs/whatsnew-3.1.rst

@@ -232,14 +232,14 @@ but if you would like to experiment with it you should now that:
 
     .. code-block:: python
 
-        from django.conf import settings
-        app.config_from_object(settings)
+        app.config_from_object('django.conf:settings')
 
     Neither will it automatically traverse your installed apps so to get the
     autodiscovery behavior of ``django-celery`` you need to call this yourself:
 
     .. code-block:: python
 
+        from django.conf imoprt settings
         app.autodiscover_tasks(settings.INSTALLED_APPS)
 
 - You no longer use ``manage.py``

+ 6 - 4
examples/django/proj/celery.py

@@ -3,12 +3,14 @@ from __future__ import absolute_import
 from celery import Celery
 from django.conf import settings
 
+app = Celery('proj.celery')
 
-celery = Celery('proj.celery')
-celery.config_from_object(settings)
-celery.autodiscover_tasks(settings.INSTALLED_APPS)
+# Using a string here means the worker will not have to
+# pickle the object when using Windows.
+app.config_from_object('django.conf:settings')
+app.autodiscover_tasks(settings.INSTALLED_APPS)
 
 
-@celery.task(bind=True)
+@app.task(bind=True)
 def debug_task(self):
     print(repr(self.request))

+ 2 - 3
examples/django/proj/settings.py

@@ -12,8 +12,8 @@ MANAGERS = ADMINS
 DATABASES = {
     'default': {
         # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
-        'ENGINE': 'django.db.backends.',
-        'NAME': '',        # path to database file if using sqlite3.
+        'ENGINE': 'django.db.backends.sqlite3',
+        'NAME': 'test.db',  # path to database file if using sqlite3.
         'USER': '',        # Not used with sqlite3.
         'PASSWORD': '',    # Not used with sqlite3.
         'HOST': '',        # Set to empty string for localhost.
@@ -117,7 +117,6 @@ INSTALLED_APPS = (
     'django.contrib.sites',
     'django.contrib.messages',
     'django.contrib.staticfiles',
-    'tasks',
     'demoapp',
     # Uncomment the next line to enable the admin:
     # 'django.contrib.admin',