GAE Go – 异步数据存储API?

huangapple go评论77阅读模式
英文:

GAE Go — Async datastore API?

问题

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

英文:

Is there a Go analogue to Python/Java's async datastore APIs? Or can one just use the normal API with the go keyword?

答案1

得分: 13

在任何AppEngine服务中,Go语言没有与Python或Java异步API相对应的功能。实际上,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() {
    // 加载用户的主要特征
    key := datastore.NewKey(ctx, "user", name, 0, nil)
    done <- datastore.Get(ctx, key)
  }()

  go func() {
    // 加载与用户关联的条目
    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
  // 并行等待查询完成
  for i := 0; i < 2 /* 计算上面的函数数量 */; i++ {
    if err := <-done; err != nil {
      ctx.Errorf("loaduser: %s", err)
      success = false
    }
  }
  if !success {
    return
  }

  // 可能还有其他内容
}

在几乎任何需要同时运行可能需要一些时间的多个任务的上下文中,都可以使用相同的方法,无论是数据存储调用、urlfetch、文件加载等。

英文:

There is no Go equivalent to the Python or Java asynchronous APIs for any AppEngine service. In fact, the Go standard library has nothing in the standard asynchronous style either. The reason is that in Go, you write functions using a blocking style and compose them using some basic concurrency primitives based on need. While you cannot just tack go at the beginning of a dastore.Get call, it is still relatively straightforward. Consider the following, contrived example:

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, &quot;user&quot;, name, 0, nil)
    done &lt;- datastore.Get(ctx, key)
  }()

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

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

  // maybe more stuff here
}

This same approach can be used in pretty much any context in which you need to run more than one thing that might take awhile at the same time, whether it's a datastore call, urlfetch, file load, etc.

答案2

得分: 2

在Go语言中没有显式的异步API。你应该使用go协程来代替。我没有看到任何关于这个的资料,但我怀疑异步API之所以不存在是因为使用go协程非常容易。

英文:

There is no explicit API for async in Go. You should use go routines instead. I haven't seen any source on this, but I suspect the async API isn't there because of how easy it is to use go routines.

huangapple
  • 本文由 发表于 2013年3月2日 03:47:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/15165656.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定