Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder2.hal-mli.net!feeder.erje.net!eu.feeder.erje.net!news-1.dfn.de!news.dfn.de!fu-berlin.de!uni-berlin.de!not-for-mail From: jt@toerring.de (Jens Thoms Toerring) Newsgroups: comp.lang.python Subject: Writing to same file from two threads Date: 26 Feb 2013 15:17:41 GMT Organization: Freie Universitaet Berlin Lines: 24 Message-ID: X-Trace: news.uni-berlin.de OEL7mm6LQlpfU79wB7BSNQwlyt5nGQJqZc4CAoxL331SXf X-Orig-Path: not-for-mail User-Agent: tin/1.9.3-20080506 ("Dalintober") (UNIX) (Linux/2.6.30-1-amd64 (x86_64)) Xref: csiph.com comp.lang.python:39961 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@toerring.de \__________________________ http://toerring.de