简体   繁体   中英

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). 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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