Restarting a Python Application

K

kyosohma

Hi,

I packaged up an application I am developing into an executable. In
the application, it has user configurable options. I would like a way
to restart the application so that the new options the user chooses
can be applied. Firefox can restart itself. Does anyone know how to
accomplish this in Python?

Here is what I tried:

<code>

exePath = os.path.join(os.getcwd(), 'myprogram.exe')
subprocess.Popen(exePath)
sys.exit()

</code>

This didn't work. It closed the program, but another instance did not
appear.

Tips would be appreciated.

Mike
 
M

Matimus

Hi,

I packaged up an application I am developing into an executable. In
the application, it has user configurable options. I would like a way
to restart the application so that the new options the user chooses
can be applied. Firefox can restart itself. Does anyone know how to
accomplish this in Python?

Here is what I tried:

<code>

exePath = os.path.join(os.getcwd(), 'myprogram.exe')
subprocess.Popen(exePath)
sys.exit()

</code>

This didn't work. It closed the program, but another instance did not
appear.

Tips would be appreciated.

Mike

You could package your program into a function or class and just
restart that.

Code:
# global flag for restart.
do_restart = False

def main(args=None):
    # program code
    # set do_restart and return to restart

if __name__ == "__main__":
    retval = main()
    while do_restart:
        retval = main()
    sys.exit(retval)
 
K

kyosohma

I packaged up an application I am developing into an executable. In
the application, it has user configurable options. I would like a way
to restart the application so that the new options the user chooses
can be applied. Firefox can restart itself. Does anyone know how to
accomplish this in Python?
Here is what I tried:

exePath = os.path.join(os.getcwd(), 'myprogram.exe')
subprocess.Popen(exePath)
sys.exit()

This didn't work. It closed the program, but another instance did not
appear.
Tips would be appreciated.

You could package your program into a function or class and just
restart that.

Code:
# global flag for restart.
do_restart = False

def main(args=None):
# program code
# set do_restart and return to restart

if __name__ == "__main__":
retval = main()
while do_restart:
retval = main()
sys.exit(retval)

Actually I am using wxPython for a GUI front-end. Thus, it is already
in a class. I am not sure how to apply your idea to a program that is
already running in an infinite event loop.

Mike
 
M

Matimus

Actually I am using wxPython for a GUI front-end. Thus, it is already
in a class. I am not sure how to apply your idea to a program that is
already running in an infinite event loop.

I don't know wxPython, but I was able to grab an example program and
adapt it to do what I think you are asking for.
Something like this:

Code:
#! /usr/bin/env python

from wxPython import wx

ID_EXIT  = 101
ID_RST   = 102

class MyFrame(wx.wxFrame):
    def __init__(self, parent, ID, title):
        wx.wxFrame.__init__(self, parent, ID, title,
                         wx.wxDefaultPosition, wx.wxSize(200, 150))
        self.CreateStatusBar()
        self.SetStatusText("This is the statusbar")

        menu = wx.wxMenu()
        menu.Append(ID_EXIT, "E&xit", "Terminate the program")
        menu.Append(ID_RST, "&Restart", "Restart the program")

        menuBar = wx.wxMenuBar()
        menuBar.Append(menu, "&File");

        self.SetMenuBar(menuBar)

        wx.EVT_MENU(self, ID_EXIT,  self.TimeToQuit)
        wx.EVT_MENU(self, ID_RST,  self.TimeToRestart)

    def TimeToQuit(self, e):
        self.Close(wx.true)

    def TimeToRestart(self, e):
        global restart
        restart = True
        self.TimeToQuit(e)


class MyApp(wx.wxApp):
    def OnInit(self):
        global restart
        restart = False
        frame = MyFrame(wx.NULL, -1, "Hello from wxPython")
        frame.Show(wx.true)
        self.SetTopWindow(frame)
        return wx.true

if __name__ == "__main__":
    MyApp(0).MainLoop()
    while restart:
        MyApp(0).MainLoop()
 
K

Kelvie Wong

Ugh.. this would be a repost for the OP, but I forgot to hit "reply to
all" again.

This should do it:

os.execv(sys.argv[0], sys.argv)

Not sure how portable that statement is, though.
 
P

Peter Decker

Ugh.. this would be a repost for the OP, but I forgot to hit "reply to
all" again.

<rant>
Imagine if you wrote applications where the default behavior did not
do what was needed 99% of the time: how long do you think you'd be in
business?

The default behavior of replying to the original sender instead of the
list makes no sense. I've read the purist arguments against replying
to the list, but I subscribed to a *list*, not an individual sender.

I know that this post is pointless, since the people who run this list
seem to care more about abstract and misdirected notions of purity
than making things as useful and intelligent as possible, but after
seeing dozens of "please keep your reply on list" responses recently,
I had to vent.
</rant>
 
S

Steve Holden

Peter said:
<rant>
Imagine if you wrote applications where the default behavior did not
do what was needed 99% of the time: how long do you think you'd be in
business?

The default behavior of replying to the original sender instead of the
list makes no sense. I've read the purist arguments against replying
to the list, but I subscribed to a *list*, not an individual sender.

I know that this post is pointless, since the people who run this list
seem to care more about abstract and misdirected notions of purity
than making things as useful and intelligent as possible, but after
seeing dozens of "please keep your reply on list" responses recently,
I had to vent.
</rant>

Well, I hope you feel better now.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------
 
B

Ben Finney

Peter Decker said:
Imagine if you wrote applications where the default behavior did not
do what was needed 99% of the time: how long do you think you'd be
in business?

You seem to be complaining about the functionality of your mail user
agent (MUA) software. The mailing list processor is doing its job
fine.

There are at least two "compose a reply" functions supported by every
MUA. Use the one that does what you want in each instance.

If you use the "Reply to individual sender" function, that's what you
get.

If you use the "Reply to all" function, that's what you get.

If you use the "Reply to the list" function, that's what you get.
The default behavior of replying to the original sender instead of
the list makes no sense.

The mailing list software provides your MUA with all the information
it needs to do what you want: the 'From' address as set by the
original poster, the 'Reply-To' address as set by the original poster,
the 'To' and 'Cc' addresses as set by the original poster, and the
'List-Post' address as set by the mailing list software. All of these
are standard message header fields with well-defined functions, and
can be used as-is to do what you need.

If your MUA isn't using this information correctly, the solution is to
fix the MUA (by choosing a better one or encouraging the maker of your
existing one to fix it), not to munge those fields to accomodate your
broken MUA.
 
C

Chris Mellon

You seem to be complaining about the functionality of your mail user
agent (MUA) software. The mailing list processor is doing its job
fine.

It's working as instructed, but that doesn't mean that it's doing the
best thing. It's common practice for mailing lists to set the reply-to
to the list itself, because that's the common case, and because it's
encouraged to keep discussion on the list.

If whoever manages the python lists doesn't want to do it, either
because they have some practical reason or because they've got a bug
up their ass about mail readers without list support, thats fine. But
it's hardly incorrect to configure it with the reply-to set to the
list, either.
 
G

Gabriel Genellina

It's working as instructed, but that doesn't mean that it's doing the
best thing. It's common practice for mailing lists to set the reply-to
to the list itself, because that's the common case, and because it's
encouraged to keep discussion on the list.

If whoever manages the python lists doesn't want to do it, either
because they have some practical reason or because they've got a bug
up their ass about mail readers without list support, thats fine. But
it's hardly incorrect to configure it with the reply-to set to the
list, either.

No, it's not correct to modify Reply-To. Some reasons:
http://www.unicom.com/pw/reply-to-harmful.html
 
C

Chris Mellon

No, it's not correct to modify Reply-To. Some reasons:
http://www.unicom.com/pw/reply-to-harmful.html

None of those are reasons, they're opinions and weary old excuses. I'm
not necessarily saying munging is the correct thing to do (although,
personally, I support it and would prefer if the python lists did it)
but it's about a clash of opinions about what cases and behaviors
should be supported and encouraged. Since it's obviously not my call
as I'm not the admin of the python lists I adjust to what the list
does, but claiming that people asking for the other behavior are
incorrect or out of line in some way is just unjustified.
 
B

Ben Finney

Chris Mellon said:
None of those are reasons, they're opinions and weary old excuses.

They're old, but not weary, because they're now supported by the
internet message standards, which spell out exactly what Reply-To and
List-Post are for.

<URL:http://woozle.org/~neale/papers/reply-to-still-harmful.html>

Reply-To, if used, is to be set by the message author. Since a
message transport system may only add fields to the header, not
arbitrarily modify existing fields, Reply-To can't be munged.
<URL:http://www.ietf.org/rfc/rfc2822.txt>

List-Post is to be used to give the email address for posting
messages to the mailing list.
<URL:http://www.ietf.org/rfc/rfc2369.txt>

Please, let's stop arguing what's already been decided. Every
behaviour you ask for is already supported by the existing
standards. The "common case" you speak of is fully supported by the
unambiguous List-Post field.

If your MUA is not using the information, provided in every message
from the mailing list, to do what you want, don't ask mailing list
admmministrators to accomodate your broken MUA.
 

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

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top