如何理解appengine ndb.tasklet? [英] how to understand appengine ndb.tasklet?

查看:97
本文介绍了如何理解appengine ndb.tasklet?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过文档


NDB tasklet是一段可能与
其他代码同时运行的代码。如果你编写一个tasklet,你的应用程序可以使用它
,就像它使用异步NDB函数一样:它调用tasklet,
返回一个Future;之后,调用Future的get_result()方法获得
的结果。

文档中的解释和示例非常喜欢对我来说很神奇。
我可以使用它,但很难正确理解它。



例如:


  1. 我可以在函数中放入任何类型的代码并将其装饰为ndb.tasklet?之后将其用作异步函数。或者它必须是appengine RPC?

  2. 这种装饰器是否也适用于我的电脑?

  3. 是否与 pypy的tasklet


解决方案

如果你看一下Future的实现,它与Python中的生成器非常相似。实际上,它使用相同的 yield 关键字来实现它所说的。请阅读介绍关于tasklets.py的评论一些澄清。

当您使用@tasklet装饰器时,它会创建一个Future并等待包装函数的值。如果该值是一个生成器,它将Future添加到事件循环中。当您在Future上 yield 时,事件循环遍历所有排队的期货,直到您想要的Future准备就绪。这里的并发是每个Future都会执行它的代码,直到它返回(使用 raise ndb.Return(...)或者函数完成),抛出异常,或者再次使用 yield 。我想技术上来说,你可以在代码中使用 yield 来停止执行该函数,以便让事件循环继续运行其他期货,但我会认为这不会帮助很大,除非你真的有一个聪明的使用案例。



回答您的问题:


  1. 技术上可以,但不会异步运行。当您使用@tasklet装饰非屈服函数时,它的Future值将在您调用该函数时进行计算和设置。也就是说,当你调用它时,它会贯穿整个函数。如果你想实现异步操作,你必须对一些做异步工作的东西产生。通常在GAE中,它将工作到RPC调用。

  2. 如果在PC上工作,你的意思是开发者应用服务器实现了tasklet / Futures,像GAE ,那么是的,虽然这对于devappserver2(现在是较新SDK中的默认值)更准确。实际上,如果本地RPC调用在使用期货时并行运行,我实际上并不是100%确定的,但是有一个eventloop正在通过期货进行调整,无论其本地还是在生产中。如果你想在你的其他非GAE代码中使用Future,那么我认为你最好使用 Python 3.2内置的未来版本(或找到后端这里


  3. 有点,它不是一个简单的比较。请看这里的文档。这个想法有些相同(调度器可以和事件回调相比较),但是低级实现大不相同。



From documentation:

An NDB tasklet is a piece of code that might run concurrently with other code. If you write a tasklet, your application can use it much like it uses an async NDB function: it calls the tasklet, which returns a Future; later, calling the Future's get_result() method gets the result.

The explanation and examples in the document really likes a magic for me. I can use it, but feel hard to understand it properly.

For example:

  1. May I put any kind of code inside a function and decorate it as ndb.tasklet? Then used it as async function later. Or it must be appengine RPC?
  2. Does this kind of decorator also works on my PC?
  3. Is it the same as tasklet for pypy

解决方案

If you look at the implementation of a Future, its very comparable to what a generator is in python. In fact, it uses the same yield keyword to achieve what it says it does. Read the intro comments on the tasklets.py for some clarification.

When you use the @tasklet decorator, it creates a Future and waits for a value on the wrapped function. If the value is a generator, it adds the Future to the event loop. When you yield on a Future, the event loop runs through ALL queued Futures until the Future you want is ready. The concurrency here is that each Future will execute its code until it either returns (using raise ndb.Return(...) or the function completes), an exception is thrown, or yield is used again. I guess technically, you can use yield in the code just to stop executing that function in favor of letting the event loop continue running other Futures, but I would assume this wouldn't help much unless you really have a clever use-case in mind.

To answer your questions:

  1. Technically yes, but it will not run asynchronously. When you decorate a non-yielding function with @tasklet, its Future's value is computed and set when you call that function. That is, it runs through the entire function when you call it. If you want to achieve asynchronous operation, you must yield on something that does asynchronous work. Generally in GAE it will work its way down to an RPC call.

  2. If by work on your PC you mean does the dev appserver implement tasklets/Futures like GAE, then yes, although this is more accurate with the devappserver2 (now the default in the newer SDK). I'm actually not 100% sure if local RPC calls will run in parallel when using Futures, but there is an eventloop going through Futures whether its local or in production. If you want to use Future's in your other, non-GAE code, then I think you would be better off using Python 3.2's built-in future (or find a backport here)

  3. Kind of, its not really a simple comparison. Look at the documentation here. The idea is somewhat the same (the scheduler can be compared to the eventloop), but the low-level implementation greatly differs.

这篇关于如何理解appengine ndb.tasklet?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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