If, like me, you shiver at the site of a While (True) loop, then you too must have thought long and hard about the best way to refactor it away. I\'ve seen several different im
The "running forever" situation is sometimes part of a larger state machine. Many embedded devices (with run-forever loops) don't really run forever. They often have several operating modes and will sequence among those modes.
When we built heat-pump controllers, there was a power-on-self-test (POST) mode that ran for a little while. Then there was a preliminary environmental gathering mode that ran until we figured out all the zones and thermostats and what-not.
Some engineers claimed that what came next was the "run-forever" loop. It wasn't really that simple. It was actually several operating modes that flipped and flopped. There was heating, and defrosting, and cooling, and idling, and other stuff.
My preference is to treat a "forever" loop as really just one operating mode -- there may be others at some point in the future.
someMode= True
while someMode:
try:
... do stuff ...
except SomeException, e:
log.exception( e )
# will keep running
except OtherException, e:
log.info( "stopping now" )
someMode= False
Under some circumstances, nothing we've seen so far sets someMode
to False
. But I like to pretend that there'll be a mode change in some future version.