How to check if there is one instance of application is already in memory

Hello.

How to check if there is one instance of application is already in memory. And if so, terminate the loading of anotherr instance. I need code both for Windows and Linux.

Thanks in advance.
Your help is very appreciated.

Best regards,
Ruslan

Hello.

How to check if there is one instance of application is already in
memory. And if so, terminate the loading of anotherr instance. I need
code both for Windows and Linux.

Sample code for the windows version can be found on www.activestate.com in the
python cookbook. It's also in O'Reilly book "Python Cookbook" page231.

···

On Wednesday 09 July 2003 02:23 am, Ruslan Spivak wrote:

Thanks in advance.
Your help is very appreciated.

Best regards,
Ruslan

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwindows.org
For additional commands, e-mail: wxPython-users-help@lists.wxwindows.org

--
  UC

--
Open Source Solutions 4U, LLC 2570 Fleetwood Drive
Phone: +1 650 872 2425 San Bruno, CA 94066
Cell: +1 650 302 2405 United States
Fax: +1 650 872 2417

Have the program open a socket on startup. If a second copy of the
program is started it will fail to open this socket. That's the first
step. The problem is that some other unrelated program may have
reserved that socket (although this is unlikely in practice). There are
a few strategies you can take at this point, the one I prefer is to have
the instance of the program that is already running listen on that
socket for connections, when the second copy starts, finds out that it
can't open that socket, try to connect to it. If it succeeds, have it
talk to the first instance to verify that it is indeed another copy of
itself. An added benefit of this is that you can use this technique to
pass information to the already running instance. For example, if the
program is run with a filename as an argument, it could pass this
filename onto the already running instance so that the first instance
could open the file.

···

On Wed, 2003-07-09 at 02:23, Ruslan Spivak wrote:

Hello.

How to check if there is one instance of application is already in
memory. And if so, terminate the loading of anotherr instance. I need
code both for Windows and Linux.

--
Cliff Wells, Software Engineer
Logiplex Corporation (www.logiplex.net)
(503) 978-6726 (800) 735-0555

Your idea certainly has benefits. But wouldn't it be less overkill to just
create a lockfile with the process id in it (or something the like) ?
Lockfiles worked well for unix in the last decade (and still do).
Also using a socket might be a problem depending on platform. Sockets don't
simply "go away" once you disconnect. They take their time depending on the
parameters in the network layer of the OS.
So you might end up with a deadlock, meaning the original copy might be gone,
but the socket is still around and can't be bound since it's in a fin_wait
state. So if you start/stop/start/stop/start your program it might not start
the second time because of the socket still lingering around

Personally I'd read the process table and check for "myself"

  UC

···

On Wednesday 09 July 2003 11:11 am, Cliff Wells wrote:

On Wed, 2003-07-09 at 02:23, Ruslan Spivak wrote:
> Hello.
>
> How to check if there is one instance of application is already in
> memory. And if so, terminate the loading of anotherr instance. I need
> code both for Windows and Linux.

Have the program open a socket on startup. If a second copy of the
program is started it will fail to open this socket. That's the first
step. The problem is that some other unrelated program may have
reserved that socket (although this is unlikely in practice). There are
a few strategies you can take at this point, the one I prefer is to have
the instance of the program that is already running listen on that
socket for connections, when the second copy starts, finds out that it
can't open that socket, try to connect to it. If it succeeds, have it
talk to the first instance to verify that it is indeed another copy of
itself. An added benefit of this is that you can use this technique to
pass information to the already running instance. For example, if the
program is run with a filename as an argument, it could pass this
filename onto the already running instance so that the first instance
could open the file.

--
Open Source Solutions 4U, LLC 2570 Fleetwood Drive
Phone: +1 650 872 2425 San Bruno, CA 94066
Cell: +1 650 302 2405 United States
Fax: +1 650 872 2417

> > Hello.
> >
> > How to check if there is one instance of application is already in
> > memory. And if so, terminate the loading of anotherr instance. I need
> > code both for Windows and Linux.
>
> Have the program open a socket on startup. If a second copy of the
> program is started it will fail to open this socket. That's the first
> step. The problem is that some other unrelated program may have
> reserved that socket (although this is unlikely in practice). There are
> a few strategies you can take at this point, the one I prefer is to have
> the instance of the program that is already running listen on that
> socket for connections, when the second copy starts, finds out that it
> can't open that socket, try to connect to it. If it succeeds, have it
> talk to the first instance to verify that it is indeed another copy of
> itself. An added benefit of this is that you can use this technique to
> pass information to the already running instance. For example, if the
> program is run with a filename as an argument, it could pass this
> filename onto the already running instance so that the first instance
> could open the file.

Your idea certainly has benefits. But wouldn't it be less overkill to just
create a lockfile with the process id in it (or something the like) ?
Lockfiles worked well for unix in the last decade (and still do).

"Well" is always a relative term. There is a small moment in time
between checking if the lockfile exists and creating it where two
instances could think they were the sole instance. This seems unlikely
but in these days of double-clicking it's fairly easy to start two
processes back-to-back.

Also using a socket might be a problem depending on platform. Sockets don't
simply "go away" once you disconnect. They take their time depending on the
parameters in the network layer of the OS.

This is usually a matter of seconds. But yes, you are correct. I've
only encountered problems like this when an application crashes without
closing the socket. I've never seen a problem outside of that. But
then, it was still only a short period (maybe 5 seconds? can't recall)
that the socket was tied up. This is still a bit better than the
lockfile solution where a crashed app often requires manually removing
the lock before another instance can run (although there are ways around
this as well, i.e. storing the PID in the lockfile is the usual way).

So you might end up with a deadlock, meaning the original copy might be gone,
but the socket is still around and can't be bound since it's in a fin_wait
state. So if you start/stop/start/stop/start your program it might not start
the second time because of the socket still lingering around

But still only for a short period (at least on Linux and Windows NT,
which are the two platforms I've tried this on).

Personally I'd read the process table and check for "myself"

That's difficult to do in a cross-platform way. Worse, with Python,
*all* instances are called "python", so it can difficult to discern
which is your application. IMHO, this should probably be considered a
bug.

Ultimately, there isn't really a foolproof way of solving this problem.
Each solution simply shuffles the same problem (lack of atomicity for
the test) elsewhere. I prefer the socket method because even in the
case of deadlock, it resolves itself after a short while without user
intervention.

Regards,

···

On Wed, 2003-07-09 at 13:04, Uwe C. Schroeder wrote:

On Wednesday 09 July 2003 11:11 am, Cliff Wells wrote:
> On Wed, 2003-07-09 at 02:23, Ruslan Spivak wrote:

--
Cliff Wells, Software Engineer
Logiplex Corporation (www.logiplex.net)
(503) 978-6726 (800) 735-0555

I thought there was an MS Windows specific solution to this that was atomic. The best solution may be separate code for Windows vs. Posix. The Windows-side might be answered best on the python-win32 list.

···

On 09 Jul 2003 13:48:45 -0700, Cliff Wells wrote:

?Ultimately, there isn't really a foolproof way of solving this
?problem. Each solution simply shuffles the same problem (lack of
?atomicity for the test) elsewhere. ?I prefer the socket method
?because even in the case of deadlock, it resolves itself after a
?short while without user intervention.

--
http://ChuckEsterbrook.com/

Possibly. IIRC (and I might not) the Windows method for determining
whether an app is running is based on the name of the app. Since all
python apps are called 'python' this will be a problem. However, IIRC,
there is also a method for setting the name of the app, so this could be
circumvented. I still suspect that there will be a brief moment in time
when the app is still called 'python' and another instance of the app is
checking to see if 'MyApp' is running and you end up with two instances.

Further, there doesn't seem to be a 'posix way' of solving this problem
(although, as Uwe mentioned, lockfiles/pidfiles seem to be the de facto
standard). Since that approach (or the socket one) works about the same
across all platforms and would have to be written anyway to support
*nix, I don't see a benefit to adding a Win32 hack in.

But then, it isn't my program, either <wink>

Regards,

···

On Wed, 2003-07-09 at 17:53, Chuck Esterbrook wrote:

On 09 Jul 2003 13:48:45 -0700, Cliff Wells wrote:
>Ultimately, there isn't really a foolproof way of solving this
>problem. Each solution simply shuffles the same problem (lack of
>atomicity for the test) elsewhere. I prefer the socket method
>because even in the case of deadlock, it resolves itself after a
>short while without user intervention.

I thought there was an MS Windows specific solution to this that was
atomic. The best solution may be separate code for Windows vs. Posix.
The Windows-side might be answered best on the python-win32 list.

--
Cliff Wells, Software Engineer
Logiplex Corporation (www.logiplex.net)
(503) 978-6726 (800) 735-0555

No, it's actually based on an arbitrary string. You attempt to create
a named mutex; if the mutex is created, you know you're first. If the
call returns a mutex and tells you that it was opened, you know
there's another app out there. It's atomic because the OS guarantees
atomicity at creation, and always destroys the mutex at process death.

···

On Thu, Jul 10, 2003 at 09:51:02AM -0700, Cliff Wells wrote:

Possibly. IIRC (and I might not) the Windows method for determining
whether an app is running is based on the name of the app.

--
Tim Lesher <tim@lesher.ws>
http://www.lesher.ws

Okay, I stand corrected. In that case it should be possible to use
something like:

http://gigue.peabody.jhu.edu/~mdboom/omi/source/shm_source/shm.html

on the Unix/POSIX side (I'm unaware of any shared memory stuff in the
standard library).

Regards,

···

On Thu, 2003-07-10 at 11:08, Tim Lesher wrote:

On Thu, Jul 10, 2003 at 09:51:02AM -0700, Cliff Wells wrote:
> Possibly. IIRC (and I might not) the Windows method for determining
> whether an app is running is based on the name of the app.

No, it's actually based on an arbitrary string. You attempt to create
a named mutex; if the mutex is created, you know you're first. If the
call returns a mutex and tells you that it was opened, you know
there's another app out there. It's atomic because the OS guarantees
atomicity at creation, and always destroys the mutex at process death.

--
Cliff Wells, Software Engineer
Logiplex Corporation (www.logiplex.net)
(503) 978-6726 (800) 735-0555

Ruslan Spivak wrote:

Hello.

How to check if there is one instance of application is already in memory. And if so, terminate the loading of anotherr instance. I need code both for Windows and Linux.

Take a look at wxSingleInstanceChecker.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

*That* is your answer? Way too simple for the real world.

Damn, I'd forgotten all about that thing.

Regards,
Cliff

···

On Sat, 2003-07-12 at 20:04, Robin Dunn wrote:

Ruslan Spivak wrote:
> Hello.
>
> How to check if there is one instance of application is already in
> memory. And if so, terminate the loading of anotherr instance. I need
> code both for Windows and Linux.
>
Take a look at wxSingleInstanceChecker.

--
I have seen too much, wipe away my eyes
                              -Bauhaus