Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #39961

Writing to same file from two threads

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 <ap420lFiectU1@mid.uni-berlin.de> (permalink)
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

Show key headers only | View raw


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

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Writing to same file from two threads jt@toerring.de (Jens Thoms Toerring) - 2013-02-26 15:17 +0000
  Re: Writing to same file from two threads Paul Rubin <no.email@nospam.invalid> - 2013-02-26 09:32 -0800
    Re: Writing to same file from two threads jt@toerring.de (Jens Thoms Toerring) - 2013-02-26 20:08 +0000
      Re: Writing to same file from two threads Antoine Pitrou <solipsis@pitrou.net> - 2013-02-27 13:26 +0000
        Re: Writing to same file from two threads jt@toerring.de (Jens Thoms Toerring) - 2013-02-27 16:05 +0000
        Re: Writing to same file from two threads Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-02-28 01:05 +0000
          Re: Writing to same file from two threads Antoine Pitrou <solipsis@pitrou.net> - 2013-03-01 14:54 +0000

csiph-web