Lifetime rules for goroutines

928 views
Skip to first unread message

Stephen

unread,
Jun 27, 2011, 6:00:22 AM6/27/11
to google-appengine-go
What are the lifetime rules for goroutines started from a request
goroutine (all requests run in their own goroutine, right)?

Experimenting, it seems that on the dev server you can start a
goroutine, return from the request, and the goroutine continues.

On the live server the second goroutine disappears - deferred funcs
are not run, no error or stack trace.

Also, an appengine.Context, which you need to do anything interesting,
is tied to a Request.

Is this all by design?

Andrew Gerrand

unread,
Jun 27, 2011, 7:26:17 AM6/27/11
to Stephen, google-appengine-go
On 27 June 2011 20:00, Stephen <sdeasey...@gmail.com> wrote:
> What are the lifetime rules for goroutines started from a request
> goroutine (all requests run in their own goroutine, right)?

A goroutine that does not return will run for as long as the app instance runs.

> Experimenting, it seems that on the dev server you can start a
> goroutine, return from the request, and the goroutine continues.
>
> On the live server the second goroutine disappears - deferred funcs
> are not run, no error or stack trace.

There is no process in place to halt those goroutines. What you
observed may have been launching a goroutine in one instance and then
hitting a different instance when you went to look for it. Another
possibility is that the instance had restarted.

You can start goroutines that run in the background, but they
generally won't be able to do anything interesting, because all API
calls are tied to a Context, which is tied to a Request.

This is all by design.

Andrew

Stephen

unread,
Jun 27, 2011, 7:50:02 AM6/27/11
to Andrew Gerrand, google-appengine-go

Then that's why it didn't work - the goroutine couldn't talk to the
datastore and log it's result using the Context passsed from the
request handler.

Regarding appengine.Context, are there any rules about how many should
be created? Should I create one at the top of a handler and pass it
around? Does it break if you don't? etc...

David Symonds

unread,
Jun 27, 2011, 8:00:41 AM6/27/11
to Stephen, Andrew Gerrand, google-appengine-go
On Mon, Jun 27, 2011 at 9:50 PM, Stephen <sdeasey...@gmail.com> wrote:

> Regarding appengine.Context, are there any rules about how many should
> be created? Should I create one at the top of a handler and pass it
> around? Does it break if you don't? etc...

You can do either way. Every call to NewContext with a particular
http.Request will return the same value.

It's probably a better idea to pass the appengine.Context around,
though, because if you start using transactions you'll need to pass
the transactional Context, which is a different object to what is
returned by NewContext.


Dave.

Reply all
Reply to author
Forward
0 new messages