开发者

GAE fetchAsync - what's the point?

开发者 https://www.devze.com 2023-04-03 09:37 出处:网络
Google encourages us to use fetchAsync instead of other means, saying that since it is asynchronous, it will开发者_如何学Go use less instance hours. However, the method returns a Future<HTTPRespons

Google encourages us to use fetchAsync instead of other means, saying that since it is asynchronous, it will开发者_如何学Go use less instance hours. However, the method returns a Future<HTTPResponse>, and you have to use the .get() method to retrieve the actual data. The .get() method is blocking, i.e. the execution of your programme will not continue until it returns (or throws an exception).

The question is: what difference does it make? Is it really possible to save the instance time without actually using Threads?


The difference is that you can go and do something else in between your fetchAsync and your get.

You can do something like (pseudo-code):

future = fetcher.fetchAsync (url)
while not future.isDone():
    doSomethingElse()
current = future.get()

Now you could achieve the same end in threaded environments by simply creating a thread to call fetch but that means managing your own threads and the interactions between them. In any case, as okrasz points out in a comment, creating new threads is not a possibility in GAE (see here).

If all you want is asynchronicity (is that even a word?) without worrying about all that extra stuff, you can use the fetchAsync/isDone/get combination as shown above.

0

精彩评论

暂无评论...
验证码 换一张
取 消