Browse Source

subtask.id is now an alias to subtask['options']['task_id']

Ask Solem 12 năm trước cách đây
mục cha
commit
e99d9771bc
1 tập tin đã thay đổi với 41 bổ sung4 xóa
  1. 41 4
      celery/canvas.py

+ 41 - 4
celery/canvas.py

@@ -32,17 +32,53 @@ Chord = Proxy(lambda: current_app.tasks['celery.chord'])
 
 
 class _getitem_property(object):
+    """Attribute -> dict key descriptor.
 
-    def __init__(self, key):
-        self.key = key
+    The target object must support ``__getitem__``,
+    and optionally ``__setitem__``.
+
+    Example:
+
+        class Me(dict):
+            deep = defaultdict(dict)
+
+            foo = _getitem_property('foo')
+            deep_thing = _getitem_property('deep.thing')
+
+
+        >>> me = Me()
+        >>> me.foo
+        None
+
+        >>> me.foo = 10
+        >>> me.foo
+        10
+        >>> me['foo']
+        10
+
+        >>> me.deep_thing = 42
+        >>> me.deep_thinge
+        42
+        >>> me.deep:
+        defaultdict(<type 'dict'>, {'thing': 42})
+
+    """
+
+    def __init__(self, keypath):
+        path, _, self.key = keypath.rpartition('.')
+        self.path = path.split('.') if path else None
+
+    def _path(self, obj):
+        return (reduce(lambda d, k: d[k], [obj] + self.path) if self.path
+                else obj)
 
     def __get__(self, obj, type=None):
         if obj is None:
             return type
-        return obj.get(self.key)
+        return self._path(obj).get(self.key)
 
     def __set__(self, obj, value):
-        obj[self.key] = value
+        self._path(obj)[self.key] = value
 
 
 class Signature(dict):
@@ -222,6 +258,7 @@ class Signature(dict):
             return self.type.apply_async
         except KeyError:
             return _partial(current_app.send_task, self['task'])
+    id = _getitem_property('options.task_id')
     task = _getitem_property('task')
     args = _getitem_property('args')
     kwargs = _getitem_property('kwargs')