EAFP vs LBYL (was Re: A little disappointed so far)
Robin Becker
robin at jessikat.fsnet.co.uk
Mon May 19 14:50:18 EDT 2003
In article <mO1ya.115460$3M4.2627893 at news1.tin.it>, Alex Martelli
<aleax at aleax.it> writes
......
>So, if A's structure is:
>
> if iswhatiwant(thefile):
> useappropriately(thefile)
> else:
> dealwithwrongness()
>
>then A is buggy. That is because, between the moment in which the test
>'iswhatiwant' runs (and returns a true value), and the later moment in
>which procedure 'useappropriately' runs, *just about anything may have
>happened* -- in particular, some other program Z might have removed or
>modified 'thefile' so that it's NOT what A wants any more. I.e., A may
>lose control of the CPU between the moment it tests and the later time
>in which it uses the result of that test.
>
>This is known as a "race condition" and it's among the hardest problems
>you may run into. A may seem to be running just fine 99 times and then
>the 100th time BOOM -- because of accidents of timing between A and
>other stuff that may be running "at the same time"... a "race", so to
>speak, whence the name whereby this horrid condition is known.
If race conditions are a problem shouldn't we be doing something like
while not havelockon(thefile):
pass # or sleep etc
useappropriately(thefile)
giveuplockon(thefile)
if we actually need to do something with/to thefile?
The real problem, for me at least, seems to be that concurrency is hard
no matter how you attempt to deal with it. I guess that race conditions
could apply to almost every file operation nowadays, but few people
program that way. Trying to deal with the race using try/except seems
harder than using an appropriate guard clause or busy loop.
-Whoops they migrated me to another processor-ly yrs-
Robin Becker
More information about the Python-list
mailing list