-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reset spyder and restart from within running application #2423
Changes from 1 commit
17395a3
01c2188
037e59d
58d3772
6c9f61f
e04aca3
519201a
bc95a72
13330fc
f0ec3f5
4003c37
28fc618
eb8b585
d341b5c
4e9b789
a6f8646
a0d7a5f
3f4b398
a738d31
4931b49
363853c
63ca7be
73eab81
37e0535
abdb8f9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ | |
|
||
|
||
PY2 = sys.version[0] == '2' | ||
CLOSE_ERROR, RESET_ERROR, RESTART_ERROR = list(range(3)) | ||
|
||
|
||
def _is_pid_running_on_windows(pid): | ||
|
@@ -63,28 +64,27 @@ def is_pid_running(pid): | |
return _is_pid_running_on_unix(pid) | ||
|
||
|
||
def error_message(type_, error=None): | ||
def launch_error_message(type_, error=None): | ||
"""Launch a message box with a predefined error message. | ||
|
||
Parameters | ||
---------- | ||
type : str ['close', 'reset', 'restart'] | ||
type : int [CLOSE_ERROR, RESET_ERROR, RESTART_ERROR] | ||
""" | ||
import spyderlib.utils.icon_manager as ima | ||
from spyderlib.baseconfig import _ | ||
from spyderlib.qt.QtGui import QMessageBox, QDialog | ||
from spyderlib.utils import icon_manager as ima | ||
from spyderlib.utils.qthelpers import qapplication | ||
|
||
messages = {'close': _("The previous instance of Spyder has not closed." | ||
"\nRestart aborted."), | ||
'reset': _("Spyder could not reset to factory defaults.\n" | ||
"Restart aborted."), | ||
'restart': _("Spyder could not restart.\n" | ||
"Restart aborted.")} | ||
titles = {'close': _("Spyder exit error"), | ||
'reset': _("Spyder reset error"), | ||
'restart': _("Spyder restart error")} | ||
|
||
messages = {CLOSE_ERROR: _("The previous instance of Spyder has not " | ||
"closed.\nRestart aborted."), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps a better message for this error would be:
|
||
RESET_ERROR: _("Spyder could not reset to factory defaults.\n" | ||
"Restart aborted."), | ||
RESTART_ERROR: _("Spyder could not restart.\n" | ||
"Restart aborted.")} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another suggestion
|
||
titles = {CLOSE_ERROR: _("Spyder exit error"), | ||
RESET_ERROR: _("Spyder reset error"), | ||
RESTART_ERROR: _("Spyder restart error")} | ||
|
||
if error: | ||
e = error.__repr__() | ||
|
@@ -101,8 +101,8 @@ def error_message(type_, error=None): | |
dlg = QDialog() | ||
dlg.setVisible(False) | ||
dlg.show() | ||
QMessageBox.warning(None, title, message, QMessageBox.Ok) | ||
raise Exception(message) | ||
QMessageBox.warning(dlg, title, message, QMessageBox.Ok) | ||
raise RuntimeError(message) | ||
|
||
|
||
# Note: Copied from py3compat because we can't rely on Spyder | ||
|
@@ -143,7 +143,7 @@ def main(): | |
# Variables were stored as string literals in the environment, so to use | ||
# them we need to parse them in a safe manner. | ||
is_bootstrap = ast.literal_eval(is_bootstrap) | ||
pid = int(pid) | ||
pid = ast.literal_eval(pid) | ||
args = ast.literal_eval(spyder_args) | ||
reset = ast.literal_eval(reset) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No problem, I just saw below that this is the case ;-) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. :-) |
||
|
||
|
@@ -180,25 +180,22 @@ def main(): | |
shell = os.name != 'nt' | ||
|
||
# Wait for original process to end before launching the new instance | ||
max_counter = 1500 # Max loops to perform before aborting | ||
counter = 0 | ||
while counter < max_counter: # This is equivalent to ~ 5 minute (1500*0.2) | ||
for counter in range(1500): # This is equivalent to ~ 5 minute (1500*0.2) | ||
if not is_pid_running(pid): | ||
break | ||
time.sleep(0.2) # Throttling control | ||
counter += 1 | ||
|
||
if counter == max_counter: | ||
error_message(type_='close') | ||
else: | ||
# The old spyder instance took too long to close and restart aborts | ||
launch_error_message(type_=CLOSE_ERROR) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now the arg is called There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oops |
||
|
||
env = os.environ.copy() | ||
|
||
reset_ok = True | ||
|
||
# Reset spyder if needed | ||
# ------------------------------------------------------------------------- | ||
reset_ok = True | ||
|
||
if reset: | ||
# Build reset command | ||
command_reset = '"{0}" "{1}" {2}'.format(python, spyder, args_reset) | ||
|
||
try: | ||
|
@@ -207,33 +204,28 @@ def main(): | |
pid_reset = p.pid | ||
except Exception as error: | ||
p.kill() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
error_message(type_='reset', error=error) | ||
launch_error_message(type_=RESET_ERROR, error=error) | ||
|
||
counter = 0 | ||
max_counter = 50 | ||
# Wait for reset process to end before restarting | ||
while counter < max_counter: # This is ~= 10 seconds (50*0.2) | ||
for counter in range(300): # This is ~= 1 minute (300*0.2) | ||
if not is_pid_running(pid_reset): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If there was an exception before, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indeed, I will make the adjustments, good suggestions |
||
break | ||
time.sleep(0.2) # Throttling control | ||
counter += 1 | ||
|
||
if counter == max_counter: | ||
else: | ||
# The rest subprocess took too long and it is killed | ||
p.kill() | ||
reset_ok = False | ||
|
||
# Restart | ||
# ------------------------------------------------------------------------- | ||
if reset_ok: | ||
# Try to restart | ||
try: | ||
p = subprocess.Popen(command, shell=shell, env=env) | ||
except Exception as error: | ||
p.kill() | ||
error_message(type_='restart', error=error) | ||
launch_error_message(type_=RESTART_ERROR, error=error) | ||
else: | ||
error_message(type_='reset') | ||
launch_error_message(type_=RESET_ERROR) | ||
|
||
|
||
if __name__ == '__main__': | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
type_
is an ugly error variable name to me. Perhapserror_type
would be better?