Restarting Python

Steven D'Aprano steve+comp.lang.python at pearwood.info
Wed Oct 1 16:47:13 CEST 2014


Seymore4Head wrote:

> Since the developers of Python decided to make Python 3 non backward
> compatible, I can't help but wonder why they don't think a command to
> restart would be a useful addition?

Possibly because it isn't a useful addition? Or maybe they just never
thought of it. But more likely the first.

Why doesn't Microsoft Office have a "Restart Office" command? Or Outlook? Or
Notepad? Normally to restart anything, with the exception of the operating
system, you exit/quit, then start again.

Inside the interactive interpreter, I can restart the interpreter with four
keystrokes:

- Ctrl-D 
- UP-ARROW 
- ENTER

Ctrl-D exits Python and returns me to the shell, UP-ARROW fetches the
previous command ("python"), and ENTER runs that command. On Windows, I
*think* you have to type Ctrl-Z ENTER instead of Ctrl-D, so that will be
five keystrokes. Either way, that's significantly faster and simpler than
using a "restart" function, say:

- type "restart()"
- ENTER

(twelve keystrokes). Even if restarting is a little harder:

- type "quit()"
- press ENTER
- click Start Menu > Programs > Python > Python33.exe

it's still not very hard in the big picture. So having a restart command
would be of very little benefit in the interactive interpreter. (I don't
say *no* benefit.) But it's actually a quite hard thing to do, restarting a
process from within itself. It can obviously be done, since I've seen
programs like Firefox do it, but I'm not sure how to write such a function.
I think that something like this might do the job:

- grab the current process ID;
- start a daemon process which monitors that process ID;
- exit;
- when the daemon process sees that process ID is no longer alive, 
  start up the application again;

but that glosses over a number of painful difficulties. What if the parent
terminal is no longer running? What about environment variables and command
line arguments? What happens if you are running over a remote shell like
ssh, and the link is terminated as soon as the Python process ends?

So "automatically restart" would be a lot of work, it would quite likely be
fragile and easily broken, and the benefit would be minimal.

But it gets worse... because restart() would also be available when running
non-interactive programs. And it isn't even clear what that should do, let
alone how to do it.

Consider a program like this:

import sys
x = 42
sys.exit()
print("Hello World")


That prints nothing, because exit() stops the program from running any
further. But what if we replace the exit() with a restart()? What would you
expect it to do?

If you expect it to restart, then continue on by printing "Hello World",
then the Python interpreter would have to be completely redesigned from
scratch. All that work, just as a convenience for restarting, is not worth
it.

I note that this sort of system can actually be justified. During the Apollo
program, the landing module's computer was designed to restart when it ran
out of memory and continue on with the current calculation that had been
interrupted:

https://www.hq.nasa.gov/alsj/a11/a11.1201-pa.html

During the eight minutes that it took for the Eagle to land on the moon, the
computer restarted at least five times, including three times in one 40
second period.

http://www.doneyles.com/LM/Tales.html


But Python is a long way from a Lunar Lander, and a restart() command would
be a lot of effort for not much benefit.


-- 
Steven




More information about the Python-list mailing list