A unique instance of Python GUI program


A

akineko

Hello everyone,

This may not be a Python specific challenge.
I have a GUI program written in Python + Tkinter.
It works very well.

Now, I would like to start it from a shell script.
As my GUI program includes a server, it should not have more than one
instance.
Is there any easy way to check if another instance of the program is
already running.

I vaguely remember that Windows programming provides a way to check.

A platform independent approach would be nice but a solution for X is
sufficient for my application.

Any comments will be greatly appreciated.

Best regards,
Aki Niimura
 
Ad

Advertisements

T

Tim Golden

akineko said:
This may not be a Python specific challenge.
I have a GUI program written in Python + Tkinter.
It works very well.

Now, I would like to start it from a shell script.
As my GUI program includes a server, it should not have more than one
instance.
Is there any easy way to check if another instance of the program is
already running.

I vaguely remember that Windows programming provides a way to check.

A platform independent approach would be nice but a solution for X is
sufficient for my application.

I swear this question's been asked twice this month already.
Is there a convention going on of one-instance-only application writers?

Have a look at:

http://mail.python.org/pipermail/python-list/2008-August/505958.html

http://mail.python.org/pipermail/python-list/2008-August/505992.html

http://mail.python.org/pipermail/python-list/2008-August/505850.html

or just search the mailing lists for various previous
discussions:

http://www.google.com/search?q=site:mail.python.org+application+single+instance

(or similar queries)

TJG
 
G

Georg Altmann

akineko said:
Hello everyone,

This may not be a Python specific challenge.
I have a GUI program written in Python + Tkinter.
It works very well.

Now, I would like to start it from a shell script.
As my GUI program includes a server, it should not have more than one
instance.
Is there any easy way to check if another instance of the program is
already running.

I vaguely remember that Windows programming provides a way to check.

On windows a mutex does the job, see CreateMutex in the windows api.
A platform independent approach would be nice but a solution for X is
sufficient for my application.

I'm not familiar with Tkinter - you might want to check if it supports
mutexes. Though I'm not sure if mutexes are cross-process (system wide)
on all platforms.

Regards
Georg
 
L

Larry Bates

akineko said:
Hello everyone,

This may not be a Python specific challenge.
I have a GUI program written in Python + Tkinter.
It works very well.

Now, I would like to start it from a shell script.
As my GUI program includes a server, it should not have more than one
instance.
Is there any easy way to check if another instance of the program is
already running.

I vaguely remember that Windows programming provides a way to check.

A platform independent approach would be nice but a solution for X is
sufficient for my application.

Any comments will be greatly appreciated.

Best regards,
Aki Niimura

Here is a recipe:

http://code.activestate.com/recipes/474070/

-Larry
 
A

akineko

I swear this question's been asked twice this month already.


Thank you very much for many pointers.
I'm awfully sorry for posting something that is already answered in
the past.
I tried to find answers to my problem using "unique instance" as a
keyword.
I guess I should have used "single instance" or "one instance",
instead.
I will try to be more careful before posting.

Thank you.
Aki Niimura
 
T

Tim Golden

akineko said:
Thank you very much for many pointers.
I'm awfully sorry for posting something that is already answered in
the past.

I was more amused than annoyed. Don't let my tone put
you off. In any case, searching is all too often a
question of knowing what the answer is so you can
search for it!

TJG
 
Ad

Advertisements

A

akineko

Again, thank you for many postings to my question.
I have reviewed solutions provided.
Well, I like the named Mutex solution under Windows.
That is a clean and straight-forward approach to the challenge.
(I cannot believe that I'm saying good thing about Windows ;-) )

Unfortunately, I'm living in Unix realm ;-)

None of solutions for Unix are appealing to me.
But they must be "the" solution for the challenge as well-established
software uses those solutions.

Now, I'm wondering.
As my program is a GUI (Tkinter) software.
Is it possible to set a known value to X11 or Tk property through
Tkinter so that another instance of the program can check if such
property is set?

Of course, I know this scheme has a flaw. If one instance uses another
logical display, then such property is probably not shared.
But for my usage, it is logically possible but not likely.

I checked my Tkinter book and found the following function.
winfo_interps(displayof=0)

This returns a list of all Tk-based applications currently running on
the display.

When I tried, I got the following:('tk #3', 'tk #2', 'tk')

But I couldn't find a way to set a specific name to the Tcl
interpreter.

As I'm not an expert of Tcl/Tk and X11, I probably overlooked other
functions that may do what I need.

Any comments, suggestions on this?
Maybe this can provide a platform independent way to ensure that only
single instance is running.

Thank you for your attention.

Best reagrds,
Aki Niimura
 
Ad

Advertisements

R

r0g

akineko said:
Again, thank you for many postings to my question.
I have reviewed solutions provided.
Well, I like the named Mutex solution under Windows.
That is a clean and straight-forward approach to the challenge.
(I cannot believe that I'm saying good thing about Windows ;-) )

Unfortunately, I'm living in Unix realm ;-)

None of solutions for Unix are appealing to me.
But they must be "the" solution for the challenge as well-established
software uses those solutions.

Now, I'm wondering.
As my program is a GUI (Tkinter) software.
Is it possible to set a known value to X11 or Tk property through
Tkinter so that another instance of the program can check if such
property is set?

Of course, I know this scheme has a flaw. If one instance uses another
logical display, then such property is probably not shared.
But for my usage, it is logically possible but not likely.

I checked my Tkinter book and found the following function.
winfo_interps(displayof=0)

This returns a list of all Tk-based applications currently running on
the display.

When I tried, I got the following:
('tk #3', 'tk #2', 'tk')

But I couldn't find a way to set a specific name to the Tcl
interpreter.

As I'm not an expert of Tcl/Tk and X11, I probably overlooked other
functions that may do what I need.

Any comments, suggestions on this?
Maybe this can provide a platform independent way to ensure that only
single instance is running.

Thank you for your attention.

Best reagrds,
Aki Niimura


I know it's a hack but couldn't you just open a specific UDP socket and
leave it dangling til your program exits, then if a new instance can't
open that same socket it can gracefully abort? If your program dies the
socket will be freed up again by the OS yes?

Just a thought.

Roger.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top