yield, curry, mix-in, new.function, global, closure, .... what will work?

ecir.hana at gmail.com ecir.hana at gmail.com
Mon Apr 16 22:15:55 EDT 2007


On Apr 17, 3:51 am, ecir.h... at gmail.com wrote:
> I'm reading the docs now and I stumbled upon something:
> section "15.3 threading -- Higher-level threading interface" mensions
> a class "local", in which "... Thread-local data are data whose values
> are thread specific. ..."
>
> Does it mean, I can create global variables whose changing is thread-
> safe?
> More specific:
>
> import threading
>
> def change_i(arg):
>     global k
>     k.i = arg
>
> def change_j(arg):
>     global k
>     k.j = arg
>
> def changer():
>     change_i('changed_i')
>     change_j('changed_j')
>
> def run(i='', j=''):
>     global k
>     k = threading.local()
>     k.i = i
>     k.j = j
>     changer()
>     i = k.i
>     j = k.j
>     return i, j
>
> print run() == ('changed_i', 'changed_j')
>
> Is this ok?

I was too quick, "k = threading.local()" has to by outside of run(),
right?




More information about the Python-list mailing list