Thread: Not restarting
View Single Post
  #1   Spotlight this post!  
Unread 16-01-2013, 23:45
tkbletsc tkbletsc is offline
Registered User
FRC #4561
 
Join Date: Jan 2013
Location: Raleigh, NC
Posts: 10
tkbletsc is just really nicetkbletsc is just really nicetkbletsc is just really nicetkbletsc is just really nice
Re: Not restarting

I haven't had a chance to test this on the robot, but given the warnings around the SystemRestart exception ("This is still not all that reliable..."), I wonder if using Python's own reload() would be better?

This works on my local machine...any reason not to do something like this? Also, anyone have an idea how to improve it?

robot.py:
Code:
from my_exceptions import RestartException
import real_code

while True:
	try:
		reload(real_code)
		real_code.go()
	except RestartException:
		print "Reloading and restarting..."
		pass
my_exceptions.py:
Code:
class RestartException(Exception): pass
real_code.py:
Code:
from my_exceptions import RestartException

import sys,os,re,time

# decide if we need to reload based on some signal, in this case a file existing 
which we delete
# on the robot, this would be a joystick check
def check_restart():
	if os.path.exists("do_reload"):
		os.unlink("do_reload")
		raise RestartException

def go():
	while True:
		check_restart()
		print "Test."
		time.sleep(1)
With the above, if I have it running in one window, edit real_code.py, then create "do_reload", the real_code.py is reloaded and restarted at the next tick.
Reply With Quote