我可以在Google App Engine(标准环境)中使用Goroutines吗? [英] Can I use Goroutines in Google App Engine (Standard Environment)?

查看:70
本文介绍了我可以在Google App Engine(标准环境)中使用Goroutines吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下示例似乎可行,但使用安全吗?我的目标是进行一些非常轻便的后台处理(而实际的任务队列作业感觉太沉重了).

The following example seems to work, but is it safe to use? My goal is to do some very light background processing (whereas an actual task queue job feels too heavy).

func MyHandler(w http.ResponseWriter, r *http.Request) {

  go func() {
    // do something ...
  }() 

  return // 200
}

推荐答案

该请求有效的Goroutines不被支持,但是您可以使用

Goroutines that outlive the request are not supported, but you can use runtime.RunInBackground to execute code in a background goroutine:

func MyHandler(w http.ResponseWriter, r *http.Request) {

  err := runtime.RunInBackground(c, func(c appengine.Context) {
    // do something...
  })

  return // 200
}

所提供的函数将在与所提供的上下文不同(并且可能比其更持久)的背景上下文中调用.请注意,每个实例最多只能有10个同时后台请求.这是另一个示例.

The provided function will be invoked with a background context that is distinct from (and may outlast) the provided context. Note that there is a limit of 10 simultaneous background requests per instance. Here is another example.

请注意,虽然支持在请求上下文内的Goroutine,但是:

Please note that Goroutines that live within the context of a request, are supported though:

App Engine的Go运行时环境完全支持 goroutine,但不适用于并行执行:已安排goroutine 到单个操作系统线程上.这种单线程限制 在将来的版本中可能会取消.可以处理多个请求 由给定实例同时进行;这意味着,如果有一个请求, 例如,等待数据存储区API调用,可能是另一个请求 由同一实例处理. ()

The Go runtime environment for App Engine provides full support for goroutines, but not for parallel execution: goroutines are scheduled onto a single operating system thread. This single-thread restriction may be lifted in future versions. Multiple requests may be handled concurrently by a given instance; that means that if one request is, say, waiting for a datastore API call, another request may be processed by the same instance. (Source)

这篇关于我可以在Google App Engine(标准环境)中使用Goroutines吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆