The Issue

While loops. You love them. You hate them if you are a plugin. When we designed our plugin, we neglected to think about the repercussions of using while loops.  This never came up when we were testing our plugin because we were still able to communicate with the printer through the server (weird). However, when we were finalizing our code trying to figure out how to get our plugin to run more than one time, Dr. Jadud noticed our lovely loops and informed us of this misfortune. Basically the trouble is that while loops will take precedent over any other plugin trying to communicate with OctoPrint and this is a problem because we want our plugin to play nice.

The Solution

The solution comes in the form of a disguised while loop pretending to be a timer. OctoPrint offers a repeated timer which is very much like spawning threads that run a function after a certain interval has passed. This is in the octoprint.util class and the documentation can be found here . Our new goal is to rewrite our plugin to replace the while loops with these Repeated Timer objects.

How it Works

If you have the following code:


This will print ‘Hello World!’ every second until it is cancelled. You can also pass conditions to a RepeatedTimer so that every time the interval is up it checks a condition and only if that condition is true is when it will actually run the function that you pass it.