我如何使这个Python函数异步? [英] How do I make this Python function asynchronous?

查看:84
本文介绍了我如何使这个Python函数异步?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为Python的新手,我发现产量和发生器以及异步函数非常令人难以接受(我已经阅读了关于主题和其他几个参考文献的所有问题 - 仍然很难连接所有的部分)。我习惯了Objective-C的描述性语法,在这里所有的东西都只是告诉你它在做什么,而不是试图连接你不知道的所有不同的关键字,他们真的知道它们是如何协同工作的。

我非常喜欢Python,因为它很容易上手 - 至少对于基本的东西来说。但对于更高级的东西,代码只是不会和你说话。它不会告诉你它在做什么。 (好吧,也许我只是有点沮丧和缺乏经验)。

无论如何,我想完全异步地完成以下功能:

  @ ndb.tasklet 
def get_new_statues(朋友):
status_list = list()
朋友中的friend_dic:
lastStatusDate = #some date
userKey = #some key
query = ndb.gql('SELECT * FROM Statuses WHERE ANCESTOR IS:1 AND date>:2',userKey,lastStatusDate)
qit = query.iter()
while(yield qit.has_next_async()):
status = qit.next()
status_list.append(status.to_dict())
提高ndb.Return(status_list)

当我写这篇文章时,我对自己有点印象深刻,很高兴能够在几个小时内将我的所有代码转换为异步。但我被告知 a>上面的函数根本不是异步的。但是,在另一个问题中 我被告知是的,我正在做的是正确的。



那究竟是怎么回事?这里的yield语句是否使我的代码同步?我怎么修改这个以使其成为异步?



(我所问的问题看起来都很相似,但问题是我得到的代码更多,而不是一个解释,我是一个寻找单词的人,理解它可以帮助我理解代码,而不是寻找更多代码来帮助我理解更多代码的机器,每个人都只是告诉我如何或什么,没有人告诉我我想为什么。)



附录:我想最让我失望的是 while(yield qit.has_next_async()):作为人类读者,我读了yield这个词,它在你的典型用法中意思是如果有必要让别的东西做别的事情我看到了异步这个词,这意味着异步。所以我认为异步方法的yield + async = magical公式,但显然不是这种情况?解析方案

听起来像你不喜欢不明白收益率是多少。



虽然这可能不完全准确,但您可以一般认为收益率与收益率一样,除非您再次调用此函数,它会继续运行,而不是函数的开始。



由于qit.has_next_async()是异步的,所以这个函数是异步的。每次你调用这个函数,它都会返回qit.has_next_function()的值,并且它会得到下一个实体。完成后,会产生一个异常,并显示结果。


As a newbie to Python, I'm finding yields and generators and async functions extremely overwhelming (I've read all the SO questions on the topics and several other various references - still having a hard time connecting all the pieces). I'm used to the descriptive syntax of Objective-C, where everything just tells you what it's doing, rather than trying to connect all the pieces of distinct keywords that you don't know really know how they might work together.

I really liked Python because it was so easy to pick up - for basic things at least. But for more advanced things, the code just doesn't speak to you. It doesn't tell you what it's doing. (Ok maybe I'm just a little frustrated and inexperienced.)

Anyway, I want to make the following function fully async:

@ndb.tasklet
def get_new_statues(friends):
    status_list = list()
    for friend_dic in friends:
        lastStatusDate = #some date
        userKey = #some key
        query = ndb.gql('SELECT * FROM Statuses WHERE ANCESTOR IS :1 AND date > :2', userKey, lastStatusDate)
        qit = query.iter()
        while (yield qit.has_next_async()):
            status = qit.next()
            status_list.append(status.to_dict())
    raise ndb.Return(status_list)

I was a little impressed with myself when I wrote this, and was excited that I was able to convert all my code to async in just a couple hours. But I'm being told that the function above isn't async at all. But then in another question I'm being told that yes, what I'm doing is correct.

So what exactly is going on? Does the yield statement here make my code synchronous? How would I modify this to make it async?

(The questions I've asked seem all similar, but the problem is I'm getting more code back as answers rather than an explanation. I'm a human looking for words to understand that can help me make sense of code, not a machine looking for more code to help me understand more code. Everyone is just telling me how or what, no one is telling me why.)

Addendum: I guess the line that's throwing me off the most is: while (yield qit.has_next_async()): As a human reader, I read the words yield, which in your typical usage of the word means "if necessary let something else do something else" and I see the word async, which well means async. So I figured yield + async = magical formula for an asynchronous method, but apparently that's not the case?

解决方案

Sounds like you don't understand what yield does.

While this may not be entirely accurate, you can generally think of "yield" like "return", except when you call this function again, it continues running from where it last yielded, rather than the beginning of the function.

Since qit.has_next_async() is asynchronous, this function is asynchronous. Every time you call this function, it'll return the value of qit.has_next_function(), and it'll get the next entity. When it's done it'll raise an exception with the result.

这篇关于我如何使这个Python函数异步?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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