Global mutex/lock

Richard Gruet rgruet at ina.fr
Wed Jan 26 10:19:08 EST 2000


<French>
(Vladimir , bonjour puisque tu écris de France, et je sais que tu es sur
python-fr)
</French>  ;-)

By global locks I mean (as you guessed) inter-process locking.

On Win32 there are synchronization objects such as mutex that fit perfectly,
but I wanted to avoid requiring the install of the Mark hammond's Win32
extensions. Unfortunately, with the solution relying on file locking via the
msvcrt module ( function: locking), I had to solve the case where the same
thread locks may times without unlocking meanwhile (the msvcrt.locking
function blocks in that case). I used the same approach than in the
threading.RLock class (a pair of variables: lockOwner, lockCount) and it
seemed to work at 1st glance. Unfortunately, more careful tests revealed that
there were some subtle race conditions that lead to interblocking, so I
eventually adopted the Win32 Mutex solution.

On Unix, the file-locking approach based on the fcntl.flock(fd, fcntl.LOCK_EX)
seems to handle correctly the pb of the recursive calls stated above.
I don't know about the existence of Python modules that would allow usage of
more 'ipc' oriented solutions.

Cheers,

Richard

Vladimir Marangozov wrote:

> Richard Gruet wrote:
> >
> > Hi Python gurus, Is there a portable global mutex implementation somewhere
> ?
>
> What do you mean by global?
>
> > AFAIK, the locks in the threading/thread modules work only between threads
> of the same module.
>
> They work for all threads within a given (Python) process, yes.
>
> > OTOH, there are file locks for Windows and Unix (respectively in modules
> msvcrt and fcntl), that can serve as locks.
>
> Yes. Locking a shared resource can be used for inter-process
> synchronization.
>
> > Am I right or am I missing something ?
>
> It depends on what you're looking for, but it sounds like you need
> inter-process synchronization of some sort... Different solutions
> exist, some are "portable", some aren't.
> For more feedback, tell us more about your problem. :-)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20000126/347c76c0/attachment.html>


More information about the Python-list mailing list