Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #92256 > unrolled thread
| Started by | Andrei <andrei.fokau@gmail.com> |
|---|---|
| First post | 2015-06-07 07:31 -0700 |
| Last post | 2015-06-12 12:08 -0700 |
| Articles | 6 — 3 participants |
Back to article view | Back to comp.lang.python
Detect if specific Python.app instance is already running Andrei <andrei.fokau@gmail.com> - 2015-06-07 07:31 -0700
Re: Detect if specific Python.app instance is already running Andrei <andrei.fokau@gmail.com> - 2015-06-07 12:54 -0700
Re: Detect if specific Python.app instance is already running Ned Deily <nad@acm.org> - 2015-06-07 16:07 -0700
Re: Detect if specific Python.app instance is already running Andrei <andrei.fokau@gmail.com> - 2015-06-08 01:32 -0700
Re: Detect if specific Python.app instance is already running Laura Creighton <lac@openend.se> - 2015-06-08 20:21 +0200
Re: Detect if specific Python.app instance is already running Ned Deily <nad@acm.org> - 2015-06-12 12:08 -0700
| From | Andrei <andrei.fokau@gmail.com> |
|---|---|
| Date | 2015-06-07 07:31 -0700 |
| Subject | Detect if specific Python.app instance is already running |
| Message-ID | <a3be1707-0eae-46fa-9921-e9c145816ad5@googlegroups.com> |
Hello,
I am experimenting with OS X apps written in Python and need to detect if there is already an instance of Python.app running with certain script. My script modifies CFBundleName on-the-fly from "Python" to "MyApp" to change the app title in the menubar. If I start another instance and check CFBundleName of the running apps, it will only tell me the original value, i.e. "Python":
for app in NSWorkspace.sharedWorkspace().runningApplications():
bundle = NSBundle.bundleWithURL_(app.bundleURL())
name = bundle.infoDictionary().get('CFBundleName')
if name in ('Python', 'MyApp'):
print name # => prints Python
So I need to find a way to mark a Python.app instance that runs MyApp script to be able to abort launching duplicate instances. Is there such way?
Thanks,
Andrei
[toc] | [next] | [standalone]
| From | Andrei <andrei.fokau@gmail.com> |
|---|---|
| Date | 2015-06-07 12:54 -0700 |
| Message-ID | <11e093d5-b78e-4ac6-9a7f-649cb2c2c942@googlegroups.com> |
| In reply to | #92256 |
Alright, I have had some development in http://stackoverflow.com/questions/30694560/detect-if-specific-python-app-instance-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. Andrei
[toc] | [prev] | [next] | [standalone]
| From | Ned Deily <nad@acm.org> |
|---|---|
| Date | 2015-06-07 16:07 -0700 |
| Message-ID | <mailman.281.1433718479.13271.python-list@python.org> |
| In reply to | #92297 |
In article <11e093d5-b78e-4ac6-9a7f-649cb2c2c942@googlegroups.com>, Andrei <andrei.fokau@gmail.com> 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, nad@acm.org
[toc] | [prev] | [next] | [standalone]
| From | Andrei <andrei.fokau@gmail.com> |
|---|---|
| Date | 2015-06-08 01:32 -0700 |
| Message-ID | <6651a781-abe1-4f25-b1f3-1f849776d060@googlegroups.com> |
| In reply to | #92312 |
On Monday, June 8, 2015 at 1:08:07 AM UTC+2, Ned Deily wrote: > In article <11e093d5-b78e-4ac6-9a7f-649cb2c2c942@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 :) Thanks, Andrei
[toc] | [prev] | [next] | [standalone]
| From | Laura Creighton <lac@openend.se> |
|---|---|
| Date | 2015-06-08 20:21 +0200 |
| Message-ID | <mailman.297.1433787707.13271.python-list@python.org> |
| In reply to | #92335 |
I needed to do something like this once. What I needed was a way to send a process a signal, and have it then spit out a huge amount of stats about how long it had been running, how many page faults it had suffered, and, goodness, I forget all the information that was needed. Lots. So I just wrapped the code in something that waited to see if it got the special signal from hell, and, when it did spit out the information. It was not pretty, but that is the sort of thing you do when somebody comes by after you have written something with some new requirements that basically mean you would have to rewrite the thing. Of course, sending odd signals to random processes in your proc table, asking them to identify themselves, runs the risk that the process isn't set up to receive signals it doesn't expect gracefully, and may fall over dead when you do so. So I am not sure I can actually recommend this technique, but it is one way to skin this particular cat. Laura
[toc] | [prev] | [next] | [standalone]
| From | Ned Deily <nad@acm.org> |
|---|---|
| Date | 2015-06-12 12:08 -0700 |
| Message-ID | <mailman.435.1434136134.13271.python-list@python.org> |
| In reply to | #92335 |
In article <6651a781-abe1-4f25-b1f3-1f849776d060@googlegroups.com>, Andrei <andrei.fokau@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@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@acm.org
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web