server bootstrapping upon connection (WARNING: LONG)

Benjamin Han this at is.for.spambot
Wed Feb 11 01:59:47 CET 2004

On 2004-02-10 18:22:30 -0500, ralf at said:

> Benjamin Han <this at is.for.spambot> writes:
>> On 2004-02-10 13:46:37 -0500, ralf at said:
>>> fortepianissimo at (Fortepianissimo) writes:
>>>> The problem: I need to prevent multiple copies of the server being
>>>> started. I did this by using file locking (fcntl.lockf()). However,
>>>> not every time the code successfully prevented the server being
>>>> started up more than one time. Here is the relevant code:
>>> When using fcntl.lockf different FooClient instances in the same
>>> process will be able lock the file and start another server. You could
>>> either use fcntl.flock to prevent that or use some global flag.
>> Hm... I didn't know there's a difference between flock() and lockf(),
>> and I didn't get much info from the document either. Could you explain
>> a bit on why lockf() would not lock the file?
> Well, it might lock the file multiple times in the same
> process. That's the problem:
> ----
> import fcntl
> def getlockfile():
>     return open('serverStart.lock', 'w')
> def getlock(f):
>     try:
>         lock(f.fileno(), fcntl.LOCK_EX|fcntl.LOCK_NB)
>     except:
>         return False
>     return True
> def doit():
>     f1 = getlockfile()
>     f2 = getlockfile()
>     print getlock(f1), getlock(f2)
> lock = fcntl.lockf
> doit()
> lock = fcntl.flock
> doit()
> ---
> Output is:
> True True
> True False

Ok I've since changed all lockf() to flock(), but from the "ps" log 
during the stress test, I still get two servers started up, listed by 
ps like this:

27308  ??  S      0:01.25 python -OO
27465  ??  SV     0:01.25 python -OO

In this case the one with pid 27308 was the first server process 
started. Some time later, with unknown reason, another server entry 
will show up in ps log, but the status is always "RV" or "SV" (e.g., 
pid 27465). The way I started the server in the code is os.system() 
call, and from the man page of ps, 'V' means "The process is suspended 
during a vfork."

The weird thing is I also have log statements in the server script, and 
from the log file only ONE copy is actually started. I start to suspect 
that I might have misunderstood the ps log - could it be there really 
is only ONE copy running, despite there're (up to) 2 entries shown in 
the ps log?

> I just wanted to say, that even if the server is
> listening on that port, that connection attempts may fail.

How is it possible? Could you explain a bit more?

Thanks again,


More information about the Python-list mailing list