Nothing has changed - you can still do this on your own by creating an ID when different goroutines are spawned, possibly as a parameter to the function.
Yes, it is great for breaking C programs when they use
multiple threads in a way that the library they call did not
anticipate. Just say no.
> For example, in a web server I create a Go routine for each user request.
> This go routine calls several functions. To each of these functions I have
> to pass the session ID which is quite nasty on the long run, especially when
> calling through libraries which are not under my control. Attaching the
> session ID to the go routine would simplify the issue a lot.
Until you want to create a second goroutine to help with the request.
Russ
Russ
Why then goroutine creation, memory allocation, garbage collection,
etc do not use explicit contexts passed via arguments? ;)
> Is there a reason for not implementing it or has nobody volunteered yet?Yes, it is great for breaking C programs when they use
> Thread local data used to be very useful from time to time.
multiple threads in a way that the library they call did not
anticipate. Just say no.
> For example, in a web server I create a Go routine for each user request.Until you want to create a second goroutine to help with the request.
> This go routine calls several functions. To each of these functions I have
> to pass the session ID which is quite nasty on the long run, especially when
> calling through libraries which are not under my control. Attaching the
> session ID to the go routine would simplify the issue a lot.
Russ
There are already goroutine-local variables: they are called
function arguments, function return values, and local variables.
Russ
Le 21/04/2011 15:57, Torben Weis a �crit :
So far I don't see the problem. Presumably the template library
is being given an interface or function value to call, and in either
case you can construct one containing the appropriate context.
Russ
Why do you think this library (which cannot handle specific parameters)
will use the same goroutine for the callback ?
So far I don't see the problem. Presumably the template library
is being given an interface or function value to call, and in either
case you can construct one containing the appropriate context.
Russ
Russ
> Of course a sensitive solution to go-routine-local-data would inherit the
> data from its parent go routine.
I've thought about that before, and it is attractive in some ways. In
Go you need that inheritance across goroutines. However, once you add
it, you can see that you are not actually talking about goroutine-local
data. You are talking about dynamic scoping, similar to dynamic scoping
in LISP implementations. Dynamic scoping is a powerful technique, but
most languages do not offer it, because most programmers find it
confusing.
The argument about difficulties in passing context is only meaningful in
Go in very special circumstances: those in which for some reason the
callee needs to call back to specific names in the caller. Most people
do not build library callbacks that way. When a library callback does
not require a specific name, then it is trivial in Go to create a
closure which includes all the context information you might want.
Ian
obviously it's possible to pass around a context,
but when searching for a bug through several third
party packages, adding a context is often much too
much work.
if it was possible to allow just this without letting
it be abused for thread-local storage, i'd definitely be in favour,
but i'm not sure it is.
Even if the go-routine ID is assigned by the runtime (i.e.
applications cannot influence the ID), then a "clever" coder can still
build a map that maps the ID to thread-local data and this app will
suffer from the confusion that Russ fears.
Thus, it is either all or nothing. Given the discussion so far it
looks more like "nothing" :-(
especially since it is hard to prove that the future harm resulting
from this feature is harmless.
Seems that the design rule for Go is: If you feel like passing around
context then do it explicitly. Do not try to attach it to a go-routine
implicitly.
Torben
> On 21 April 2011 21:06, Ian Lance Taylor <ia...@google.com> wrote:
>> Torben Weis <torbe...@gmail.com> writes:
>>
>>> Of course a sensitive solution to go-routine-local-data would inherit the
>>> data from its parent go routine.
>>
>> I've thought about that before, and it is attractive in some ways. In
>> Go you need that inheritance across goroutines. However, once you add
>> it, you can see that you are not actually talking about goroutine-local
>> data. You are talking about dynamic scoping, similar to dynamic scoping
>> in LISP implementations. Dynamic scoping is a powerful technique, but
>> most languages do not offer it, because most programmers find it
>> confusing.
>>
>> The argument about difficulties in passing context is only meaningful in
>> Go in very special circumstances: those in which for some reason the
>> callee needs to call back to specific names in the caller. Most people
>> do not build library callbacks that way. When a library callback does
>> not require a specific name, then it is trivial in Go to create a
>> closure which includes all the context information you might want.
>>
>> Ian
>>
>
--
First of all, it is not the only use case as discussed before.
Second, I sincerely wish you a job where you can always reject using
some existing code base because it is not designed the way it could
have been in a perfect world.
Torben
We can do that. We're working on a new language.
If we had to accept everything that already existed
we'd still be using C++.
Russ
Sure. However, a good language supports reuse of code instead of
enforcing a redesign.
For example GO's interfaces allow me to take some object that has
never explicitly implemented some interface and cast it to this
interface nevertheless.
That's extremely cool, because I do not have to change the code of
this other object to explicitly mention the new interface.
One could even argue that it is dangerous, because the object might
implement the interface syntactically, but the semantics are
different.
Nevertheless, I guess nobody who has ever written Go would like to
loose this feature.
Passing data along a call chain implicitly (i.e. by attaching it to a
go-routine) is a tool that follows the same goal: Make it possible to
use code that is there and that has not explicitly being designed for
this purpose. And yes, there is a chance that something goes wrong.
I fully accept if the Go designers say the benefit does not value the
risk of something going wrong.
Most choices in new languages are not really ground breaking new.
Most of them are old choices, but new languages have the freedom to
choose a different answer.
BTW: If passing context along in local variables is the best way of
doing it, why does UNIX feature a function to obtain the PID? You
could pass it to the main function and from there to the place where
you need it. This way your code does not get confused if the PID
suddenly changes after a fork :-)
My last mail on this topic, promised.
Cheers
Torben
> Russ
>
Sure. However, a good language supports reuse of code instead of
enforcing a redesign.
unix provides getpid so you can do things with a process
without its consent - you can kill it, set its priority, query its
statistics etc.
in Go you can do none of those things.
BTW: If passing context along in local variables is the best way of
doing it, why does UNIX feature a function to obtain the PID?