芹菜获取和加入之间的区别 [英] Difference between celery get and join

查看:70
本文介绍了芹菜获取和加入之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

之间是否存在任何差异

 r = group(some_task.s(i) for i in range(10)).apply_async()
 result = r.join()

并且:

 r = group(some_task.s(i) for i in range(10))()
 result = r.get()

Celery文档同时使用了两个示例,我看不出有什么区别。

Celery document uses both examples and I do not see any difference.

推荐答案

简短答案

的> get 和 join 方法应返回相同的结果, get 实现了一些缓存,根据您使用的后端,效率可能更高。除非在某些情况下确实需要使用 join ,否则应使用 get

While the get and join methods for a group should return the same results, get implements some caching and will probably be more efficient depending on the backend you're using. Unless you really need to use join for some edge case, you should use get.

长答案

以下是获取芹菜的 ResultSet 类的方法,该方法由 GroupResult 类扩展。

Here is the source for the get method of celery's ResultSet class which the GroupResult class extends.

def get(self, timeout=None, propagate=True, interval=0.5,
        callback=None, no_ack=True, on_message=None):
    """See :meth:`join`
    This is here for API compatibility with :class:`AsyncResult`,
    in addition it uses :meth:`join_native` if available for the
    current result backend.
    """
    if self._cache is not None:
        return self._cache
    return (self.join_native if self.supports_native_join else self.join)(
        timeout=timeout, propagate=propagate,
        interval=interval, callback=callback, no_ack=no_ack,
        on_message=on_message,
    )

我们首先看到的是文档字符串告诉我们查看 join 方法进行文档编制。马上,这表明方法非常相似。

The first thing we see is that the docstring is telling us to look at the join method for documentation. Right off the bat, this is an indication that the methods are very similar.

查看 get 方法,我们可以看到它首先检查缓存的值,如果已设置,则返回该值。如果找不到缓存的值,则 get 将调用 join join_native 方法,具体取决于后端是否支持本机联接。如果您发现该 return 语句的格式有些混乱,则本质上是同一件事:

Looking at the body of the get method, we can see that it first checks for a cached value and returns that if it's set. If no cached value is found, get will call either the join or the join_native method depending on whether the backend supports native joins. If you find the format of that return statement a little confusing, this is essentially the same thing:

if self.supports_native_join:
    return self.join_native(timeout=timeout,
                            propagate=propagate,
                            interval=interval,
                            callback=callback,
                            no_ack=no_ack,
                            on_message=on_message)
else:
    return self.join(timeout=timeout,
                     propagate=propagate,
                     interval=interval,
                     callback=callback,
                     no_ack=no_ack,
                     on_message=on_message)

join 方法的文档字符串具有这样的意思。

The docstring for the join method has this to say.


此对于必须使用
进行轮询的结果存储后端而言,这可能是一项昂贵的操作(例如数据库)。如果后端支持,则应考虑使用
join_native

因此,如果后端支持,则应该调用 join_native 而不是 join 。但是,如果 get 为您总结了这个逻辑,为什么还要费心地有条件地调用一个或另一个呢?只需使用 get 即可。

So you should be calling join_native instead of join if your backend supports it. But why bother resorting to conditionally calling one or the other if get wraps up this logic for you? Just use get instead.

这篇关于芹菜获取和加入之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆