Package init() occurs twice

1,100 views
Skip to first unread message

Philipp Schumann

unread,
Jun 6, 2013, 1:38:19 PM6/6/13
to google-ap...@googlegroups.com
At least in the GAE Go SDK for Win64, locally when running with dev_appserver.py:

Here's my simple package:


When I run with dev_appserver.py, the func init() is called once. Outputting: "This is init #1"

Then I may wait a few seconds or minutes or not at all before sending the first http request via browser --- this repeats the whole init() procedure, but it does NOT recognize the first run as the code should.

I would expect it to output "This is init #2" but I'm seeing yet another "This is init #1"

So essentially looks like the whole process is restarted, and all the 1st-time init work was for naught.

(However, all subsequent http requests do indeed run without doing repeated inits --- as they properly should, too.)

Begs the question: if the very first http requests is what seems to be starting the executable, what was the point of the first init()?

Andrew Gerrand

unread,
Jun 6, 2013, 7:21:00 PM6/6/13
to Philipp Schumann, google-appengine-go

On 7 June 2013 03:38, Philipp Schumann <philipp....@gmail.com> wrote:
I would expect it to output "This is init #2" but I'm seeing yet another "This is init #1"

That can never happen. The init functions in a Go program are only ever executed once.

The dev_appserver is supposed to compile and run the binary on the first HTTP request. I'm not sure why it would be starting it before then, but there's nothing inherently wrong with doing that. App instances are started and restarted at the discretion of the app engine infrastructure; as a developer you shouldn't need to think about it.

Andrew

David Symonds

unread,
Jun 6, 2013, 7:21:53 PM6/6/13
to Philipp Schumann, google-ap...@googlegroups.com
init functions are called once per program invocation:
http://golang.org/ref/spec#Program_execution

On App Engine, your app instance may be shut down at any time, and new
instances started at any time, but the init functions will still only
be run once per instance.

The dev appserver will rebuild and restart your app when it thinks it
is needed. It'll rebuild if you touch any source code (or anything in
GOPATH), and will restart if you touch any file in your app (to cover
changes to template files, for instance).

Philipp Schumann

unread,
Jun 7, 2013, 4:12:20 AM6/7/13
to google-ap...@googlegroups.com, Philipp Schumann
OK, so that's by design more or less, makes sense, can work with that.

So I guess I shouldn't do too much work in init() and "init on the fly / lazily / on demand" as ongoing requests dictate. That's the smart approach anyway, so it's all good.

Günter Zöchbauer

unread,
Sep 12, 2013, 2:40:43 AM9/12/13
to google-ap...@googlegroups.com, Philipp Schumann
This seems to say the opposite

Moving the root app files incl. app.yaml to a subdirectory (without further subdirectories) solved the problem for me.
All imported packages are then siblings and not subdirectories to the app directory.
Since then I had no duplicate initializations.
Reply all
Reply to author
Forward
0 new messages