Reputation: 6785
In my tornado server I have a background process that runs periodically. I implemented this as a never-ending loop as recommended:
@tornado.gen.coroutine
def background_loop():
while True:
do_something()
yield tornado.gen.sleep(60)
(and separately I call)
ioloop.spawn_callback(background_loop)
Now to my question -- sometimes I want to trigger an immediate run of the background loop. The problem, I can't have two of these run in parallel as the code assumes that only one loop runs at a time.
I'm wondering, is there a way to "wake up" my background loop?
My current hack is that I implemented some locks, launch a second background_loop that will run for one iteration steals the lock and the main loop will skip an iteration if it cannot acquire the lock. Feels like it would be much simpler to just wake up the main method though...
Thx!
Upvotes: 1
Views: 96
Reputation: 22154
The simplest answer is to use a Queue: Instead of gen.sleep()
, use Queue.get
with a timeout. The timeout will fire periodically, or you can wake it up immediately by putting a value in the queue.
Upvotes: 1