Reputation: 537
I want to run a timer in a tornado based web app such that the it runs in background and is non blocking.
Once the timer finishes, a specific task has to be called so it is very important that the timer completes exactly on time.
What should be the ideal way to do it ?
I read Tornado IOLoop.spawn_callback
in the documentation but I am not very clear that it would behave correctly.
I don't quite understand the statement in the doc
Unlike all other callback-related methods on IOLoop, spawn_callback does not associate the callback with its caller’s stack_context
Upvotes: 0
Views: 743
Reputation: 21834
If you want to run a function after a specific time, you can use IOLoop.call_later
. Use it like this:
IOLoop.current().call_later(5, my_func) # will call my_func 5 seconds later
def my_func():
# do something
IOLoop.spawn_callback
is used for running a callback/function in the next iteration of the IOLoop, that is - almost instantly. You can't add a time out to spawn_callback
. Since you want to schedule a callback after a timeout, IOLoop.call_later
is what you need.
In your comment you asked
Why according to you
IOLoop.spawn_callback
is not to be used?
Well, I never said to not use it. You can use it if you need it. In this case, you don't.
So, when do you need it? When you'll need to run a callback almost instantly, without a timeout, that's when you can use spawn_callback
. But even then, there's IOLoop.add_callback
which is used much more widely than spawn_callback
.
Upvotes: 2