[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Python threading and sharing variables

On Wed, Jul 5, 2017 at 6:57 PM, Thomas Nyberg <tomuxiong at gmx.com> wrote:
> On 07/05/2017 10:40 AM, Chris Angelico wrote:
>> What would the lock surround?
> Sorry yes I agree with you that no lock is needed in this method. I was
> a bit confused by the code and probably was thinking something like "a
> += 1" in my head (even though that's not what he was doing...). Thanks
> for clearing that up!
> Regardless, depending upon what he means by this, he'll probably need
> some form of syncronization at some point:

It all depends on the meaning of "and control" from the OP. The
example code is all about getting status, which can always be done
atomically (if you need a complex object, you simply construct a new
one every time, atomically place it in the shared location, and on
reading, always take a reference atomically before digging deeper into
it), but other types of command-and-control would need more

As a general rule, I try to avoid using locks *per se* in my code.
It's usually clearer to have clear communication channels, generally
unidirectional. Locks are reserved for the weird cases where you're
doing something really unusual.