Detect if specific Python.app instance is already running

Ned Deily nad at acm.org
Fri Jun 12 15:08:35 EDT 2015


In article <6651a781-abe1-4f25-b1f3-1f849776d060 at googlegroups.com>,
 Andrei <andrei.fokau at gmail.com> wrote:

> On Monday, June 8, 2015 at 1:08:07 AM UTC+2, Ned Deily wrote:
> > In article <11e093d5-b78e-4ac6-9a7f-649cb2c2c942 at googlegroups.com>,
> >  Andrei wrote:
> > > Alright, I have had some development in 
> > > http://stackoverflow.com/questions/30694560/detect-if-specific-python-app-
> > > inst
> > > ance-is-already-running and can prevent running multiple instances of the 
> > > same app/script (by lockf), but I still need to identify which Python.app 
> > > instance is running certain script. I guess it's more Cocoa-related 
> > > question 
> > > but I hope someone had same problem with tkinter etc.
> > 
> > Can you give a more complete explanation of the goal, i.e. what you are 
> > trying to end up with here, rather than the steps you are taking to get 
> > there?  It's not at all clear to me what your goal is and the cumbersome 
> > steps you find you have to take suggest that you might be going down the 
> > wrong path.  OS X apps usually don't have to resort to such hacks.  If 
> > you haven't already, make sure you have looked at the "Information 
> > Property List Key Reference" for OS X (and iOS) apps.  For instance, key 
> > LSMultipleInstancesProhibited *might* be useful if you really only want 
> > to ensure that there is only instance of an app launched.
> > 
> > https://developer.apple.com/library/mac/documentation/General/Reference/I
> > nfoPlistKeyReference/Articles/LaunchServicesKeys.html
> > 
> > -- 
> >  Ned Deily
> 
> 
> Hi Ned,
> Thanks for your reply! The problem is a bit tricky as I see it. Multiple 
> instances of Python.app needs to be run since it may run different tkinter 
> apps etc. What I need is rather simple:
> 
> 1. start app, mark the instance with tkinter app name
> 2. start another one, detect that the same instance is already running and 
> identify it
> 3. focus on the first instance, close the second one
> 
> I have solved it with a lock file and a pid file (I guess I could use just 
> one file for both jobs, but didn't have time to test it yet). I believe it 
> could be done without files, i.e. by setting some public attributes on app 
> instances. I could probably also use OS X key storage for this but it gets 
> too similar to the file approach and I am not sure if it's any better.
> 
> I may be wrong in my thinking. If so, just let me know :)

If you have something that works for you, great!  A question that 
remains is: how are you launching all these apps?  You mention tkinter 
so presumably these are "gui" apps.  On OS X, native "gui" apps are 
expected to be packaged as app bundles and launched via Launch Services, 
e.g. by double-clicking in the Finder or by /usr/bin/open from the 
command line.  Framework installs of Python cheat a bit and provide a 
default Python.app bundle buried in the framework and do some trickery 
when python is started conventionally from the command line (e.g. via 
something like /usr/local/bin/python) to allow the process to be 
promoted to a gui app, if necessary, like when using Tk from tkinter.  
If you need gui apps, the most "Mac-thonic" approach would be to use 
something like py2app to produce each different app as a unique gui app 
bundle and then, assuming each app's Info.plist are defined properly, OS 
X will enforce that only one instance of each app is running at a time.  
But that works only if they are launched as apps rather than run as 
scripts from the Python interpreter.  Of course, this all may be 
overkill for what you are trying to do but dynamically modifying app 
bundles or some such certainly does not sound like a good idea for lots 
of reasons.  

In any case, good luck!

-- 
 Ned Deily,
 nad at acm.org




More information about the Python-list mailing list