在 ndb tasklet 中使用 images.get_serving_url_async() 是否安全? [英] Is it safe to use images.get_serving_url_async() inside a ndb tasklet?

查看:14
本文介绍了在 ndb tasklet 中使用 images.get_serving_url_async() 是否安全?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

NDB tasklets 和 yield 是执行异步/并行代码的好方法.但是,文档中并不清楚这种机制是否可以安全地用于非 ndb 异步函数,例如 images.get_serving_url_async().

NDB 异步操作文档页面有一个非常小的关于使用 NDB 上下文自己版本的 urlfetch_async() 的部分,其中说明(强调我的):

<块引用>

网址提取服务有自己的异步请求 API.很好,但与 NDB tasklets 一起使用并不总是那么容易.

我不太清楚为什么与 NDB tasklets 一起使用并不总是那么容易",这让我想知道同样的声明是否适用于 images.get_serving_url_async().>

所以我的问题是:如果我这样做,我会遇到麻烦吗?

@ndb.tasklet定义 foo():url = 产量图像.get_serving_url_async(image_key)

解决方案

没问题.yield 将等待 RPC 并允许其他小任务在等待时运行.关于 urlfetch 的评论只是关于其笨拙的 API.

NDB tasklets and yield are a great way to do async/parallel code. However, it is not quite clear from the documentation if this mecanism can be safely used with non-ndb async functions such as images.get_serving_url_async().

The NDB Asynchronous Operation documentation page has a very tiny section about using the NDB context's own version of urlfetch_async(), where it is stated (emphasis mine):

The URL Fetch service has its own asynchronous request API. It's fine, but not always easy to use with NDB tasklets.

It is not quite clear to me why it is "not always easy to use with NDB tasklets", and this makes me wonder if the same statement applies to images.get_serving_url_async().

So my question is: will I get into trouble if I do this?

@ndb.tasklet
def foo():
    url = yield images.get_serving_url_async(image_key)

解决方案

It is fine. The yield will wait for the RPC and allow other tasklets to run while waiting. The comment about urlfetch was just about its clumsy API.

这篇关于在 ndb tasklet 中使用 images.get_serving_url_async() 是否安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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