@@ -237,92 +237,6 @@ named :file:`tasks.py`:
237237 >>> add.name
238238 'tasks.add'
239239
240- .. _task-naming-relative-imports :
241-
242- Automatic naming and relative imports
243- -------------------------------------
244-
245- .. sidebar :: Absolute Imports
246-
247- The best practice for developers targeting Python 2 is to add the
248- following to the top of **every module **:
249-
250- .. code-block :: python
251-
252- from __future__ import absolute_import
253-
254- This will force you to always use absolute imports so you will
255- never have any problems with tasks using relative names.
256-
257- Absolute imports are the default in Python 3 so you don't need this
258- if you target that version.
259-
260- Relative imports and automatic name generation don't go well together,
261- so if you're using relative imports you should set the name explicitly.
262-
263- For example if the client imports the module ``"myapp.tasks" ``
264- as ``".tasks" ``, and the worker imports the module as ``"myapp.tasks" ``,
265- the generated names won't match and an :exc: `~@NotRegistered ` error will
266- be raised by the worker.
267-
268- This is also the case when using Django and using ``project.myapp ``-style
269- naming in ``INSTALLED_APPS ``:
270-
271- .. code-block :: python
272-
273- INSTALLED_APPS = [' project.myapp' ]
274-
275- If you install the app under the name ``project.myapp `` then the
276- tasks module will be imported as ``project.myapp.tasks ``,
277- so you must make sure you always import the tasks using the same name:
278-
279- .. code-block :: pycon
280-
281- >>> from project.myapp.tasks import mytask # << GOOD
282-
283- >>> from myapp.tasks import mytask # << BAD!!!
284-
285- The second example will cause the task to be named differently
286- since the worker and the client imports the modules under different names:
287-
288- .. code-block :: pycon
289-
290- >>> from project.myapp.tasks import mytask
291- >>> mytask.name
292- 'project.myapp.tasks.mytask'
293-
294- >>> from myapp.tasks import mytask
295- >>> mytask.name
296- 'myapp.tasks.mytask'
297-
298- For this reason you must be consistent in how you
299- import modules, and that is also a Python best practice.
300-
301- Similarly, you shouldn't use old-style relative imports:
302-
303- .. code-block :: python
304-
305- from module import foo # BAD!
306-
307- from proj.module import foo # GOOD!
308-
309- New-style relative imports are fine and can be used:
310-
311- .. code-block :: python
312-
313- from .module import foo # GOOD!
314-
315- If you want to use Celery with a project already using these patterns
316- extensively and you don't have the time to refactor the existing code
317- then you can consider specifying the names explicitly instead of relying
318- on the automatic naming:
319-
320- .. code-block :: python
321-
322- @app.task (name = ' proj.tasks.add' )
323- def add (x , y ):
324- return x + y
325-
326240 .. _task-name-generator-info :
327241
328242Changing the automatic naming behavior
0 commit comments