OS independent way to check if a python app is running?

Hans Mulder hansmu at xs4all.nl
Tue Dec 22 18:27:26 EST 2009


Dan Sommers wrote:
> On Mon, 14 Dec 2009 14:14:05 -0500, python wrote:
> 
>> Is there an os independent way to check if a python app is running?
>>
>> Goal: I have a server program based on cherrypy that I only want to have
>> running once. If a system administrator accidentally attempts to run
>> this program more than once, I would like the 2nd instance of the
>> program to detect that its already running and exit.
> 
> Maybe I'm missing something, but the locking mechanism already exists:  
> at some point, your server program has to bind to an IP port to listen 
> for incoming request, and any respectable OS won't let two programs bind 
> to the same port at the same time.

Unfortunately, Windows is not a respectable OS.  Unlike Unix, it allows
two processes to bind to the same port.  The theory is that this somehow
allows the two processes to share their workload.

One thing the OP can portably do, is try to connect() to the port.
If that succeeds, then a server program is already running at that port,
so he should exit.


Hope this helps,

-- HansM



More information about the Python-list mailing list