If you don't need to take away user control, there's a very easy way to do this: Create a threading.Timer
.
What you want to do is take the "continuation" of the function—that is, everything that would come after the time.sleep
—and move it into a separate function my_function
, then schedule it like this:
threading.Timer(60, my_function).start()
And at the end of my_function
, it schedules a new Timer
with the exact same line of code.
Timer
is a pretty clunky interface and implementation, but it's built into the stdlib. You can find recipes on ActiveState and modules on PyPI that provide better classes that, e.g., run multiple timers on one thread instead of a thread per timer, let you schedule recurring calls so you don't have to keep rescheduling yourself, etc. But for something that just runs every 60 seconds, I think you may be OK with Timer
.
One thing to keep in mind: If the background job needs to deal with any of the same data the user is dealing with in the REPL, there is a chance of a race condition. Often in an interactive environment (especially in Python, thanks to the GIL), you can just lay the onus on the user to not cause any races. If not, you'll need some kind of synchronization.
Another thing to keep in mind: If you're trying to do GUI work, depending on the GUI you're using (I believe matplotlib
is configurable but defaults to tkinter
?), you may not be able to update the GUI from a background thread.
But there's actually a better solution in that case anyway. GUI programs have an event loop that runs in some thread or other, and almost every event loop ever design has a way to schedule a timer in that thread. For tkinter
, if you have a handle to the root
object, just call root.after(60000, my_function)
instead of threading.Timer(60, my_function).start()
, and it will run on the same thread as the GUI, and without wasting any unnecessary resources.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…