class mutex

Olivier Dagenais olivierS.dagenaisP at canadaA.comM
Sat Oct 21 16:33:15 EDT 2000


I looked into this when I had to synchronize CGI processes.  The best idea I
have found is to try to open a file in write mode when someone else has
already opened it in write mode and has made it read-only.  It didn't work
with the built-in open function, but it did when I tried the low-level
sys.open (I think that's the one, maybe it was os.open??)..

I've attached vaisselle.py (it means "dishes" in french), which contains the
class "Chopstick" - which does the actual locking and blocking - (I got the
idea for the name after the "dining philosophers problem") and the secret
class "Napkin" which attempts to maintain a queue of the requests made to
the lock.

The "proc.py" example program creates a 3-meg file, after making sure it has
acquired the "cs1" lock.  Start a few instances of it in different windows
and see what happens!

It's not very elegant, but it worked on my Win95 machine.  I wanted to test
it on a Unix machine, but I didn't get around to it.  I make no guarantees
about that code, it was purely experimental and I'm not so such some of the
operations are "atomic enough" to be a good semaphore.

Good luck, I've now switched to using threads to avoid this mess.  I may get
back to it later, in the future...

--
----------------------------------------------------------------------
Olivier A. Dagenais - Software Architect and Developer
"Someone called 'Type your name here' is impersonating me on the
internet and is posting exactly the same things I am posting!"


"Prof. Peter Stoehr" <peter.stoehr at weihenstephan.org> wrote in message
news:39F1E7B8.D2E0F2AB at weihenstephan.org...
> Hi out there,
>
> there is a class mutex shipped with python. I've tried to use it to
> syncronise two processes created with os.fork() and failed :-(
> Looking at the source code of mutex.py I found out, that it is
> implemented in pure python. Thus, it does not implement a real
> mutex-object as a python code can be interrupted after each step of the
> byte-code.
> Based on that, I have two questions:
> 1) Does python provide a real mutex object.
> 2) What is the use of the class mutex
>
>     Greeting
>         Peter
> --
> --------------------------------------------------------------------------
-
>   Prof. Peter Stoehr --- Teisenbergweg 6 --- 85435 Erding ---
> 08122/47232
>       http://www.weihenstephan.org/home/ak/support/team/jpg/peter.jpg
> --------------------------------------------------------------------------
-
> I'm the terror that flaps through the night


begin 666 proc.py
M:6UP;W)T('9A:7-S96QL92P@;W,-"@T*8W,Q(#T@=F%I<W-E;&QE+D-H;W!S
M=&EC:R H(")C<S$B("D-"@T*<&ED(#T@;W,N9V5T<&ED("@@*0T*8W,Q+G=A
M:70@*" I#0IP<FEN=" B1&]N92!W86ET:6YG(2(-"FA&:6QE(#T@;W!E;B H
M(")S>6YC:"YT>'0B+" B82(@*0T*:$9I;&4N=W)I=&4@*" B4')O8V5S<R G
M)74G(&ES('-T87)T:6YG+BXN7&XB("4@<&ED("D-"F9O<B!C;W5N=&5R(&EN
M(')A;F=E("@@,3 P,# P("DZ#0H):$9I;&4N=W)I=&4@*" B<&ED(#T@)74L
M(&-O=6YT97(@/2 E9%QN(B E("@@<&ED+"!C;W5N=&5R("D@*0T*#0IC<S$N
.<VEG;F%L("@@*0T*#0H`
`
end

begin 666 vaisselle.py
M:6UP;W)T('1E;7!F:6QE+"!O<RP@=&EM90T*#0IF86QS92 ]("@@-#(@(3T@
M-#(@*0T*=')U92 ]("@@-#(@/3T at -#(@*0T*<F5T<GE4:6UE(#T@,0T*#0IC
M;&%S<R!#:&]P<W1I8VLZ#0H)9&5F(%]?:6YI=%]?("@@<V5L9BP@;F%M92 I
M. at T*"0ES96QF+F9I;&5N86UE(#T@<F5P<B H('1E;7!F:6QE+G1E;7!D:7(@
M*2 K(&YA;64-"@D)<V5L9BYN87!K:6X@/2!.87!K:6X@*"!S96QF+F9I;&5N
M86UE("D-"@D)<V5L9BYH1FEL92 ]($YO;F4-"@D)<V5L9BYP:60@/2!O<RYG
M971P:60@*" I#0H-"@ED968@=V%I=" H('-E;&8@*3H-"@D)<&ED(#T@<V5L
M9BYP:60-"@D)=V]R:V5D(#T@=')U90T*"0ET<GDZ#0H)"0ES96QF+FA&:6QE
M(#T@;W,N;W!E;B H('-E;&8N9FEL96YA;64L(&]S+D]?4D174B!\(&]S+D]?
M0U)%050@?"!O<RY/7U1254Y#+" P-# P("D-"@D)97AC97!T. at T*"0D)=V]R
M:V5D(#T at 9F%L<V4-"@D)"7-E;&8N;F%P:VEN+G)E9VES=&5R("@@<&ED("D-
M"@T*"0EW:&EL92 H(&YO="!W;W)K960@*3H-"@D)"71I;64N<VQE97 @*"!R
M971R>51I;64@*0T*"0D):68@*"!S96QF+FYA<&MI;BYI<TYE>'0@*"!P:60@
M*2 I. at T*"0D)"71R>3H-"@D)"0D)=V]R:V5D(#T@=')U90T*"0D)"0ES96QF
M+FA&:6QE(#T@;W,N;W!E;B H('-E;&8N9FEL96YA;64L(&]S+D]?4D174B!\
M(&]S+D]?0U)%050@?"!O<RY/7U1254Y#+" P-# P("D-"@D)"0D)<V5L9BYN
M87!K:6XN=6YR96=I<W1E<B H('!I9" I#0H)"0D)97AC97!T. at T*"0D)"0EW
M;W)K960@/2!F86QS90T*"0D)96QS93H-"@D)"0EP87-S#0H-"@ED968@<VEG
M;F%L("@@<V5L9B I. at T*"0EO<RYC;&]S92 H('-E;&8N:$9I;&4@*0T*"0EO
M<RYC:&UO9" H('-E;&8N9FEL96YA;64L(# R,# @*0T*#0IC;&%S<R!.87!K
M:6XZ#0H)9&5F(%]?:6YI=%]?("@@<V5L9BP@;F%M92 I. at T*"0ES96QF+F9I
M;&5N86UE(#T@;F%M92 K("<N<75E)PT*"0ES96QF+FA&:6QE(#T at 3F]N90T*
M#0H)9&5F(')E9VES=&5R("@@<V5L9BP@<&ED("DZ#0H)"7-E;&8N:$9I;&4@
M/2!O<&5N("@@<V5L9BYF:6QE;F%M92P@)V$K)R I#0H)"7-E;&8N:$9I;&4N
M<V5E:R H(# L(#(@*0T*"0ES96QF+FA&:6QE+G=R:71E("@@<F5P<B H('!I
M9" I("L@)UQN)R I#0H)"7-E;&8N:$9I;&4N8VQO<V4@*" I#0H-"@ED968@
M:7-.97AT("@@<V5L9BP@<&ED("DZ#0H)"7-E;&8N:$9I;&4@/2!O<&5N("@@
M<V5L9BYF:6QE;F%M92P@)W(G("D-"@D);&EN92 ]('-E;&8N:$9I;&4N<F5A
M9&QI;F4@*" I#0H)"7-E;&8N:$9I;&4N8VQO<V4@*" I#0H)"7)E='5R;B H
M(&QI;F4@(#T]("@@<F5P<B H('!I9" I("L@)UQN)R I("D-"@T*"61E9B!U
M;G)E9VES=&5R("@@<V5L9BP@<&ED("DZ#0H)"7-E;&8N:$9I;&4@/2!O<&5N
M("@@<V5L9BYF:6QE;F%M92P@)W(G("D-"@D)8V]N=&5N=',@/2!S96QF+FA&
M:6QE+G)E861L:6YE<R H("D-"@D)<V5L9BYH1FEL92YC;&]S92 H("D-"@D)
M;W,N<F5M;W9E("@@<V5L9BYF:6QE;F%M92 I#0H)"7-E;&8N:$9I;&4@/2!O
M<&5N("@@<V5L9BYF:6QE;F%M92P@)W<K)R I#0H)"7-E;&8N:$9I;&4N=W)I
M=&5L:6YE<R H(&-O;G1E;G1S6S$Z72 I#0H)"7-E;&8N:$9I;&4N8VQO<V4@
'*" I#0H-"@``
`
end




More information about the Python-list mailing list