[Tutor] fcntl: Blocking or non-blocking

Ignacio Vazquez-Abrams ignacio@openservices.net
Wed, 29 Aug 2001 01:50:18 -0400 (EDT)


On Tue, 28 Aug 2001, Sheila King wrote:

> OK, I've been pestering the python-list/comp.lang.python with my
> questions about file locking, and now that I'm down to some more
> elementary concepts (that illustrate my fundamental cluelessness with
> *nix systems), I've decided to give them a break and pester you
> kind-hearted and patient folks. ;)
>
> Here's the deal:
> I'm writing a module that will perform file locking. I want it to work
> on both Windows and Unix. This requires platform specific stuff, so I
> write two separate modules, and then wrap them in one cross-platform
> module with a common interface.
>
> OK, I've got that all down, now, I think.
> But one question remains:
>
> On Unix I have a choice of using blocking or non-blocking calls to fcntl
> to get the file lock.
>
> Blocking obviously saves me the effort of having to code retries (which
> I have to do for the Windows version, anyhow).
>
> But I have a concern: what if I try for a blocking call, and the process
> never acquires the lock? (Maybe I should specify, that these are for CGI
> scripts, anyhow. So, I guess eventually the process will time out and
> die.) Should I just code the blocking calls? Or should I code
> non-blocking and have retries with sleep in there? I can't see any easy
> way in Python to use the fcntl.lockf command and get a blocking call to
> time out.

Blocking in general is Evil unless you know exactly what you're doing. In
fact, it is SO Evil that Unix developed the select() function (replicated in
the select module) specifically for getting around blocking calls. So I would
say that you should raise an exception or return None instead of ever
blocking.

> Anyone interested in the 50+ messages on this topic in comp.lang.python,
> can look at the two threads that start with these messages:
>
> http://groups.google.com/groups?q=hl=en&selm=djsfotomogdeba70gp1cuogjbt5ii3chrb%404ax.com
>
> http://groups.google.com/groups?hl=en&selm=bo2ootg61vmposfvsg8gvrks6eqiuss940%404ax.com

Oh don't worry, it'll keep going strong there too ;)

> --
> Sheila King
> http://www.thinkspot.net/sheila/
> http://www.k12groups.org/

-- 
Ignacio Vazquez-Abrams  <ignacio@openservices.net>