Python threading and sharing variables

Chris Angelico rosuav at gmail.com
Wed Jul 5 09:03:52 EDT 2017


On Wed, Jul 5, 2017 at 10:14 PM, Peter Otten <__peter__ at web.de> wrote:
> Chris Angelico wrote:
>
>> You can be confident that a single assignment will happen atomically.
>> Even if "self.cnt = i" requires multiple instructions to perform
>
> For name binding
>
> cnt = i
>
> maybe, but
>
> self.cnt = i
>
> can execute arbitrary Python code (think __setattr__()). With threads I'd
> rather play it safe.

Sure, it _could_ execute arbitrary code, but the most likely case is
that at its core, it's still going to execute a single assignment
operation. And if it doesn't, then that's the place where you'd need
the lock.

ChrisA



More information about the Python-list mailing list