A unique instance of Python GUI program

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • akineko

    A unique instance of Python GUI program

    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
  • Tim Golden

    #2
    Re: A unique instance of Python GUI program

    akineko wrote:
    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:







    or just search the mailing lists for various previous
    discussions:



    (or similar queries)

    TJG

    Comment

    • Georg Altmann

      #3
      Re: A unique instance of Python GUI program

      akineko schrieb:
      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

      Comment

      • Larry Bates

        #4
        Re: A unique instance of Python GUI program

        akineko wrote:
        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:



        -Larry

        Comment

        • akineko

          #5
          Re: A unique instance of Python GUI program

          On Sep 16, 1:58 am, Tim Golden <m...@timgolden .me.ukwrote:
          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

          Comment

          • Tim Golden

            #6
            Re: A unique instance of Python GUI program

            akineko wrote:
            On Sep 16, 1:58 am, Tim Golden <m...@timgolden .me.ukwrote:
            >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 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

            Comment

            • akineko

              #7
              Re: A unique instance of Python GUI program

              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(d isplayof=0)

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

              When I tried, I got the following:
              >>root.winfo_in terps()
              ('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

              Comment

              • r0g

                #8
                Re: A unique instance of Python GUI program

                akineko wrote:
                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(d isplayof=0)
                >
                This returns a list of all Tk-based applications currently running on
                the display.
                >
                When I tried, I got the following:
                >>>root.winfo_i nterps()
                ('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.

                Comment

                • Grant Edwards

                  #9
                  Re: A unique instance of Python GUI program

                  On 2008-09-16, akineko <akineko@gmail. comwrote:
                  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 unix the solution is usually a lockfile placed in a
                  predefined location. Other people use a socket, but that's a
                  bit more susceptible to namespace collisions with unrelated
                  programs.
                  A platform independent approach would be nice but a solution
                  for X is sufficient for my application.
                  I don't see what X has to do with it.
                  Any comments will be greatly appreciated.
                  This question is asked and answered about once a week, and I'm
                  always surprised at how frequently it comes up. I've been
                  doing sw development for decades and apart from Unix system
                  daemons, I don't remember ever needing to prevent multiple
                  instances of an application from running. Can somebody lend me
                  a clue as to why this question comes up so often? There can't
                  be that many people writing Unix daemons in Python (and if they
                  were, they'd probably already know about lockfiles).

                  Just curious...

                  --
                  Grant Edwards grante Yow! Is something VIOLENT
                  at going to happen to a
                  visi.com GARBAGE CAN?

                  Comment

                  Working...