Writing to same file from two threads

Jens Thoms Toerring jt at toerring.de
Tue Feb 26 21:08:05 CET 2013


Paul Rubin <no.email at nospam.invalid> wrote:
> jt at toerring.de (Jens Thoms Toerring) writes:
> > in garbled output (i.e. having some output from A inside a
> > line written by B or vice versae) because the "main thread" or

> Yes they do get garbled like that.  Preferred Python style is put a
> single thread in charge of all the i/o to that file, and communicate
> with it by message passing through Queue objects.  That is safer than
> directly using locks.

Thank you for confirmig my suspicion;-) But you have induced
another question: why is using a Queue safer than locking (not
that I doubt that it might be more elegant etc.). Is it "safer"
because it's less likely that one gets it wrong (e.g. by for-
grtting to acquire the lock) or is there something inherently
unsafe about locks?

                  Thank you and best regards, Jens
-- 
  \   Jens Thoms Toerring  ___      jt at toerring.de
   \__________________________      http://toerring.de



More information about the Python-list mailing list