In the Python blog, there was a
discussion about the design of infinite loops, which is more aesthetic: while True or while 1 <5 :-) This topic is my experience and reasoning on the topic of infinite loops and their implementation in python. Sorry that I can not publish in Python blog - so I am writing here.
In my opinion, working with such cycles is based on two pillars:
Kit 1: They do not exist. Yes, the endless cycle is a kind of Cheshire cat, which can disappear unnoticed by the programmer, leaving a smile. And so that from your program and the results of its work also did not remain one smile (but rather dissatisfied with the chief / customer's grimace), it is necessary to observe a certain caution. Each infinite loop once began, which means that someday it will end, most importantly, that it ends when it is convenient for you, and not the data provider, the user who clicked Ctrl + C, or the flown UFO. Design exit points from the loop. Most often this will be the end of the iteration, however a way out of the middle is possible. And sometimes there will be such cases that before going out, blood from the nose, you need to do some actions, maybe even one or two iterations. This means that the exit from the cycle must be firmly held in your hands and not be left to third-party libraries.
Kit 2: Down with the magic. By “magic,” I understand the use of non-standard “mego-cool” geek things, the behavior of which you cannot predict at 100%. I myself like to write a few lines so that everything will be done right away. The question is, will this mega-coolness always behave steadily and predictably when the initial conditions change? If you have not studied them thoroughly, can you use more classical approaches more reliably and efficiently? But it turns out that they saved 10 lines, got a lot of crap and crashes at critical moments, and in the end, to get rid of them, we added another 50 lines of code :)
As you already understood, “magic” is a relative concept. When I first started learning python, I and the construction of the form
url = self._url + (http_params and (glue + '&'. join (['% s =% s'% (k, v) for k, v in http_params.items ()]))) or '')
seemed like magic.
')
Based on this, I developed my approach to creating infinite loops. An auxiliary variable is started, you can call it whatever you like, I usually call working, then everything is simple:
working = True
while working:
...
And then when we need to change working = False, the cycle ends up to the end, and the next one does not start. But the approach is flexible - you can not just get out of the cycle, but do some other actions, go out in the middle (if not working: break) or even scroll through one or two more cycles - all as required by the task.
Moreover, it is not necessary to change this variable in the cycle itself. You can hang the handler on the OS signals and, by a certain signal, reset this flag. So you can make a nice exit from the cycle, so that when you press Ctrl + C, the treysbek does not fall out on the screen, and all the accumulated data is thrown together with it into the tartare. So it is possible to bring the work to a logical point, save files, record all changes in the database and exit beautifully, waving a pen to the user.
This approach has proven itself as flexible and reliable, and is actively used in different demons.
What approaches do you use?
This is my first habratopic - wishes and comments are welcome