gpt4 book ai didi

google-app-engine - GAE Go——异步数据存储 API?

转载 作者:IT王子 更新时间:2023-10-29 01:16:39 25 4
gpt4 key购买 nike

是否有类似于 Python/Java 的异步数据存储 API 的 Go 类比?或者可以只使用带有 go 关键字的普通 API 吗?

最佳答案

对于任何 AppEngine 服务,没有与 Python 或 Java 异步 API 等效的 Go。事实上,Go 标准库也没有标准异步风格。原因是在 Go 中,您使用阻塞风格编写函数,并根据需要使用一些基本的并发原语组合它们。虽然您不能在 dastore.Get 调用的开头添加 go,但它仍然相对简单。考虑以下人为的示例:

func loadUser(ctx appengine.Context, name strings) (*User, err) {
var u User
var entries []*Entry
done := make(chan error)

go func() {
// Load the main features of the User
key := datastore.NewKey(ctx, "user", name, 0, nil)
done <- datastore.Get(ctx, key)
}()

go func() {
// Load the entries associated with the user
q := datastore.NewQuery("entries").Filter("user", name)
keys, err := q.GetAll(ctx, &entries)
for i, k := range keys {
entries[i].key = k
}
done <- err
}()

success := true
// Wait for the queries to finish in parallel
for i := 0; i < 2 /* count the funcs above */; i++ {
if err := <-done; err != nil {
ctx.Errorf("loaduser: %s", err)
success = false
}
}
if !success {
return
}

// maybe more stuff here
}

同样的方法几乎可以用于您需要同时运行多个可能需要一段时间的任何上下文,无论是数据存储调用、urlfetch、文件加载等。

关于google-app-engine - GAE Go——异步数据存储 API?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15165656/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com