1

Very general question, but: does there exist a way to "globally" define a specific user input that will, no matter where it occurs - assuming predetermined raw_input points - restart the entire script?

Something that might allow for the statement "If at any point you wish you restart, enter 'restart'", as an example.

Thanks very much for any insight.

glarue
  • 530
  • 7
  • 20
  • What sort of application is this? – James Mills Dec 10 '13 at 03:34
  • Technically, yes, but it's not a good idea. It messes with parts of the system you didn't write that don't expect this behavior, or even parts of the system that you did write when you weren't thinking about this part. – user2357112 Dec 10 '13 at 03:43
  • just write your own wrapper for `raw_input` that does that. – roippi Dec 10 '13 at 03:44
  • @JamesMills currently, it's a program which is automating a pipeline to fetch some biological data off of government servers; while not absolutely necessary to implement, because I've got a rather long series of decisions which need to be made before the program downloads data, I thought it would be helpful to allow the user to bail out in an "official" way rather than just closing the command window. Just curious if this was something that is done commonly or not. – glarue Dec 11 '13 at 17:42
  • @roippi But would that `raw_input` wrapper not have to be included at every point of user input? – glarue Dec 11 '13 at 17:46

1 Answers1

0

The traditional way to do what you want is to have the code listen for SIGHUP. The SIGHUP handler could restart the code as you want. A separate (possibly forked) process, with a user-friendly interface could be made to issue the signal on demand.

Information on writing python signal handlers can be found here.

Community
  • 1
  • 1
John1024
  • 109,961
  • 14
  • 137
  • 171
  • Thanks for this. I shall do some reading! Do you know whether this sort of thing is, as suggested by a user above, generally frowned upon for some reason? – glarue Dec 11 '13 at 17:45
  • To the contrary, the ability to handle signals, such as [SIGINT, SIGTERM, and SUGHUP on Unix](http://en.wikipedia.org/wiki/Unix_signal) or their [windows equivalents](http://stackoverflow.com/questions/16826097/equivalent-to-sigint-posix-signal-for-catching-ctrlc-under-windows-mingw), is normal and expected and particularly so for a program that is long running and/or might leave files that need to be cleaned up. – John1024 Dec 11 '13 at 19:36