[Python-Dev] thread semantics for file objects

Aahz aahz at pythoncraft.com
Thu Mar 17 23:27:13 CET 2005


On Thu, Mar 17, 2005, Tim Peters wrote:
>
> I think Aahz was on target here:
> 
>     NEVER, NEVER access the same file object from multiple threads, unless
>     you're using a lock.
> 
> And here he went overboard:
> 
>     And even using a lock is stupid.
> 
> ZODB's FileStorage is bristling with locks protecting multi-threaded
> access to file objects, therefore that can't be stupid.  QED

Heh.  And how much time have you spent debugging race conditions and
such?  That's the thrust of my point, same as we tell people to avoid
locks and use Queue instead.  I know that my statement isn't absolutely
true in the sense that it's possible to make code work that accesses
external objects across threads.  (Which is why I didn't garnish that
part with emphasis.)  But it's still stupid, 95-99% of the time.

Actually, I did skip over one other counter-example: stdout is usually
safe across threads provided one builds up a single string.  Still not
something to rely on.
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

"The joy of coding Python should be in seeing short, concise, readable
classes that express a lot of action in a small amount of clear code -- 
not in reams of trivial code that bores the reader to death."  --GvR


More information about the Python-Dev mailing list