How safe is modifying locals()?
Paul Paterson
paulpaterson at users.sourceforge.net
Fri Jul 25 22:01:33 EDT 2003
Steven Taschuk wrote:
> Quoth Paul Paterson:
> [...]
>
>>Does anyone have any alternative strategies for trying to achieve the
>>objective? A "Pythonic" approach such as,
>>
>>def change(x, y):
>> x = x + 1
>> y = y + 1
>> return y
>>
>>x = 0; y = 0
>>y = change(x, y)
>>
>>Works in a single threaded environment but, because the value of 'y'
>>changes at the wrong time, with multiple threads there is a possibility
>>that two users of 'y' could dissagree on its value.
>
>
> Even if the simple
> y = y + 1
> could change the caller's binding, there would be a race condition
> -- you'll need a lock anyway. (I'd be a bit surprised if this
> were not so in VB as well. Are statements atomic in VB?)
>
AFAIK in VB, if 'y' is a global (or in some other way accessible via two
threads) then concurrent changes are managed behind the scenes by VB -
presumably either some locking is occuring or the assignment statement
is atomic. To be honest though, I'm not sure. Threading usually occurs
either by using timers or COM. I have seen issues where you have to
manually implement semaphores when using timers and COM together.
That asside, you make an excellent point. The thread safety and ByRef
issues are probably orthogonal and I should probably look for two
complementary solutions.
More information about the Python-list
mailing list