GAE中func init()的第二次执行

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

Second Execution of func init() in GAE

问题

我有一个非常简单的代码,我的.go文件如下:

func init() {
    http.HandleFunc("/", handlerMain)
    log.Println("init executed")
}

func handlerMain(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "TEST")
}

还有app.yaml文件:

application: newsboard
version: 1
runtime: go
api_version: go1 

handlers:
- url: /.* 
  script: _go_app

当首次执行时,一切都正常,控制台输出如下:

INFO     2015-10-19 19:28:56,626 devappserver2.py:763] Skipping SDK update check.
INFO     2015-10-19 19:28:56,652 api_server.py:205] Starting API server at: http://localhost:56946
INFO     2015-10-19 19:28:56,655 dispatcher.py:197] Starting module "default" running at: http://localhost:8080
INFO     2015-10-19 19:28:56,658 admin_server.py:116] Starting admin server at: http://localhost:8000
2015/10/19 19:28:59 init executed

但是当我访问http://localhost:8080时,我得到以下结果:

INFO     2015-10-19 19:32:16,394 module.py:786] default: "GET / HTTP/1.1" 200 4
2015/10/19 19:32:16 init executed

所以init()函数被执行了两次。然后,每次重新加载页面时,一切都正常,控制台不再显示"init executed"。我的问题是:为什么init()会执行两次,这样做是否正常?

英文:

I have very simple code, my .go file:

func init() {
http.HandleFunc("/", handlerMain)
log.Println("init executed")
}

func handlerMain(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "TEST")
}

and app.yaml:

application: newsboard
version: 1
runtime: go
api_version: go1 

handlers:
- url: /.* 
  script: _go_app

When Executed at first everything goes fine, this is output from console

INFO     2015-10-19 19:28:56,626 devappserver2.py:763] Skipping SDK update check.
INFO     2015-10-19 19:28:56,652 api_server.py:205] Starting API server at: http://localhost:56946
INFO     2015-10-19 19:28:56,655 dispatcher.py:197] Starting module "default" running at: http://localhost:8080
INFO     2015-10-19 19:28:56,658 admin_server.py:116] Starting admin server at: http://localhost:8000
2015/10/19 19:28:59 init executed

But when I Enter http://localhost:8080 I get following:

INFO     2015-10-19 19:32:16,394 module.py:786] default: "GET / HTTP/1.1" 200 4
2015/10/19 19:32:16 init executed

So init() somehow is being executed twice. Then, Every time when I reload page everething is ok,"init executed" doesn't appear in console anymore.
My question: Why init() happens twice and is this okay?

答案1

得分: 2

这是完全没问题的。

Go AppEngine SDK会监视应用程序的代码库,每当检测到更改(例如修改了一个.go源文件),它会自动重新加载应用程序。这包括卸载代码并重新加载它-再次运行init()函数。

基本上,应用程序何时重新加载(因此执行init()函数)的控制权在SDK的“手中”。尝试修改一个.go文件,然后在浏览器中刷新:你会看到另一个init()函数的执行。

当你首次启动应用程序时,所有的代码都会被加载/编译(并执行init()函数)-这需要做以报告是否有错误。之后,SDK可能会卸载它(不确定-找不到相关文档),只有在发生请求时才会重新加载它(可能很快,也可能不会)。

英文:

It is perfectly fine.

The Go AppEngine SDK monitors the application's code base, and whenever it detects changes (e.g. you modified a .go source file) it automatically reloads your application. This includes / involves unloading your code and reloading it - running init() functions again.

Basically control over when an app is reloaded - and therefore execution of the init() functions - is in the "hands" of the SDK. Try to modify a .go file and hit refresh in the browser: you will experience another init() function execution.

When you first start your app, all the code is loaded/compiled (and init() functions get executed) - this needs to be done in order to report if there are errors. After that the SDK may unload it (not sure - couldn't find relevant docs) and only load it again if a request does happen (which may or may not come anytime soon).

huangapple
  • 本文由 发表于 2015年10月20日 03:40:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/33222556.html
匿名

发表评论

匿名网友

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

确定