Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

alternative to python's time.sleep()

I'm performing realtime data processing + display, and I hit our database every 60 seconds. I'd like to not use time.sleep() for waiting every 60 seconds, as it removes control from me (namely REPL access to variables, which isn't necessary but nice) and freezes matplotlib charts.

Is there an alternative? Ideally, something that would initially give control to the user, and after 60 seconds, take control away, run some code, and update a plot, then give control back to the user. (When I say control, I mean REPL control).

Any ideas?

like image 391
Cam.Davidson.Pilon Avatar asked Mar 01 '13 21:03

Cam.Davidson.Pilon


1 Answers

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.

like image 139
abarnert Avatar answered Sep 19 '22 16:09

abarnert