EAFP vs LBYL

Jorge Godoy godoy at metalab.unc.edu
Mon May 19 09:30:10 EDT 2003


Alex Martelli <aleax at aleax.it> writes:

<snip />

> Fortunately, in a language with good support for exceptions such as
> Python, you are NOT doomed to enter the hell of race conditions -- just
> use EAFP instead of LBYL:
>
>     try:
>         useappropriately(thefile)
>     except ItWasWrong, howwasitwrong:
>         dealwithwrongness()
>
> See how deeply simpler this is?  'useappropriately' just ASSUMES the
> file is 'what A wants' and raises an ItWasWrong exception if the
> assumption proves to be unfounded.  You don't have to code a
> separate 'iswhatiwant' test -- what you DO want is determined inherently
> by what 'useappropriately' tries to do.  No race conditions, no code
> that must duplicate the set of conditions to be checked for, no
> duplicate work at runtime in terms of system calls to determine
> if a condition holds followed by system calls to take advantaqe
> of that condition.

<snip />


Alex, how would that work with two concurrent applications using a DB
file (both accessed through anydbm module)?

One think I had to do in other languages was to use a lockfile. I did
the same in Python, but this explanation has tempted me to rewrite the
code using the EAFP approach. 

The code I wrote has the following specs:

    1. Web interface for the users to ask for a service
    2. The web interface schedules the operation on a DBM file
    3. A manually run program creates a lockfile, reads the DBM file,
       performs the operation and remove the entry from it
    4. The program removes its lockfile when the last entry in the DBM
       file is dealt with, making it possible, again, to use the web
       interface

What I want is to make the scheduling always possible --- I even
thought about using separate files on a spool directory, but then I'd
be unfair with users who asked first for the operation to be performed
(or I'd have to add timestamp tests) --- without having to worry if
the data integrity of the DBM file is OK.

The operation may take several minutes (actually, the minimum is near
10 minutes) for each user and requires a media change (this is why it
is manually run). 



TIA,
-- 
Godoy.      <godoy at metalab.unc.edu>

Give your child mental blocks for Christmas.




More information about the Python-list mailing list