简体   繁体   English

如何使此Python函数异步?

[英]How do I make this Python function asynchronous?

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). 作为Python的新手,我发现yields和generators和async函数非常不堪重负(我已经阅读了有关该主题的所有SO问题以及其他几个参考-仍然很难连接所有部分)。 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. 我习惯了Objective-C的描述性语法,其中的所有内容仅告诉您它在做什么,而不是尝试将您不知道的所有不同关键词完全联系在一起。

I really liked Python because it was so easy to pick up - for basic things at least. 我真的很喜欢Python,因为它很容易拿起-至少对于基本的东西而言。 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? 此处的yield语句会使我的代码同步吗? 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. 附录 :我想最让我失望的是: while (yield qit.has_next_async()):作为人类读者,我读过yield一词,在您通常的用法中,它的意思是“如有必要,让其他东西做其他事情”,我看到了async这个词,也就意味着async。 So I figured yield + async = magical formula for an asynchronous method, but apparently that's not the case? 所以我想出了异步方法的yield + async =神奇公式,但是显然不是这样吗?

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. 由于qit.has_next_async()是异步的,因此此函数是异步的。 Every time you call this function, it'll return the value of qit.has_next_function(), and it'll get the next entity. 每次调用此函数时,它将返回qit.has_next_function()的值,并获取下一个实体。 When it's done it'll raise an exception with the result. 完成后,它将引发异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM