need dict to maintain order

Jason Orendorff jason at jorendorff.com
Sun Jan 20 14:00:08 EST 2002


Jason Orendorff wrote:
>  if not os.path.isdir(self.dirname):
>      os.mkdir(self.dirname)

Alex Martelli wrote:
> try:
>     os.mkdir(self.dirname)
> except OSError, err:
>     if err.errno != errno.EEXIST: raise

Hmm.  Or,

try:
    os.mkdir(self.dirname)
except OSError, err:
    if not os.path.isdir(self.dirname): raise

This also raises if self.dirname exists but is not a directory.
You could call this style LAYL, "look after you leap".  :)

Alex Martelli wrote:
> The defect is a *race condition*.  Two different processes could
> be running at the same time (or in some slightly different cases
> two threads of the same process) and both be trying to run code
> very similar to this one.  If one is switched out and the other
> switched in between the if and its body (will happen rarely, which
> gives race conditions their elusive nature) then both processes
> will see the "not ... isdir" guard satisfied, so both will execute mkdir,
> but only one of them will succeed -- the other one crashes with
> EEXIST, and it's 3 AM call time.

First and most important, this program is not designed to behave
intelligently if some unknown other process is mucking about in
the same directory at the same time.  In the absence of such a
design goal, one should write the clearest possible code.  Period.

Second, under your version *both* processes continue,
opening all the same files and overwriting each other's work
helter-skelter.  Still a race condition.

The behavior of this program *will* be flaky if its actions
conflict with those of another program.  It would take some
serious work to make it otherwise, and an EAFTP approach is
neither necessary nor sufficient for that.

> Wrong.  When you do proceed, you'll almost certainly get an
> exception very soon; all that you've lost by using careless
> EAFTP is that the error diagnosis comes a little bit later, a little bit 
> less clearly.  You still do catch the error if you do decent testing
> (and if you don't, you have worse problems than these:-).  It's
> nowhere as bad as the race conditions enticed by LBYL.

I maintain that correct error reporting in the likely case
("permission denied" or "parent directory doesn't exist") is
more important than having your program exhibit flavor A
flaky behavior instead of flavor B in a not-designed-for
multiprocess scenario.

## Jason Orendorff    http://www.jorendorff.com/




More information about the Python-list mailing list