[Python-ideas] Fwd: Concurrent safety?

Terry Reedy tjreedy at udel.edu
Wed Nov 2 23:28:15 CET 2011


On 11/2/2011 5:27 AM, Paul Moore wrote:

> I'm not sure what you mean here. Suppose I have
>
> l = [1,2,3]
> for i in l:
>    print(i)
>
> Here, the thing you need to lock is not l, as it's not being mutated,

If l is exposed to another thread, it can be mutated, and the hidden 
iterator in the for loop will work, but with indeterminant, or at least 
unexpected results. Were you implicitly excluding such exposure?

(A list can also be mutated within its iteration loop. There is even a 
use case for deleting an item while iterating in reverse.)

Dicts *are* locked for iteration because mutating a hash array during 
iteration could have more drastic effects and there is no good use case. 
A built-in subclass of list could use the same mechanism as dict for 
locking during iteration.

> but the temporary iterator generated by the for loop. That's not
> exposed to the user, so you can't lock it manually. Should it be
> locked? It can never be seen from another thread.

So no need to lock *it*.

-- 
Terry Jan Reedy




More information about the Python-ideas mailing list