Writing to same file from two threads

Jens Thoms Toerring jt at toerring.de
Tue Feb 26 16:17:41 CET 2013


Hi,

   I noticed in someone elses program that it writes single
lines to the same file from (what I call for loss of a better
name) the "main thread" of the program and from a thread sub-
sequentally started. This got me worried if it might result
in garbled output (i.e. having some output from A inside a
line written by B or vice versae) because the "main thread" or
the other thread could be interrupted during a call of write().
Is this a valid concern (and thus locking the file object is
required before writing to it) or am I guaranteed that this
can't happen? In the latter case I would be grateful for an
explanation what mechanism is responsible for this never to
happen.
                  Thanks and best regards, Jens

PS: I already have determined experimentally that a context
    switch definitely can happen between two calls of write()
    (and I expected nothing else), what I'm worried about are
    context switches somewhere within the very innards of what
    write() does.
-- 
  \   Jens Thoms Toerring  ___      jt at toerring.de
   \__________________________      http://toerring.de



More information about the Python-list mailing list