Re: [tornado] Gracefully Stop PeriodCallback During Shutdown

174 views
Skip to first unread message

Jon Parise

unread,
Aug 22, 2012, 3:42:26 PM8/22/12
to python-...@googlegroups.com
On Wed, Aug 22, 2012 at 11:03 AM, Calvin <calvin...@gmail.com> wrote:
> Hi all,
>
> I have a PeriodicCallback that makes HTTP requests to an external API via
> SimpleAsyncHTTPClient. Whenever a request is made, it's important to record
> the response in the datatabase; missing a response is bad.
>
> Whenever I shut down the Tornado server, I run the risk of doing so in the
> middle of a request, which would cause my application to miss recording a
> response. Is there a way to gracefully shut down Tornado, but block if the
> PeriodicCallback is currently running?

I use a graceful shutdown process in my application. It blocks for up
to 10 seconds while it waits for IO loop handlers to complete.

Here's the code: https://gist.github.com/3428652

Calvin

unread,
Aug 22, 2012, 5:44:22 PM8/22/12
to python-...@googlegroups.com
Ahh this is awesome -- looks like what I was looking for.

But I do one question about how it works.  On line 20 of your gist, you check that there's only a single element in the _handlers list before stopping the ioloop.  Reading through the ioloop code, I can see that PeriodicCallbacks push _Timeouts onto the _timeout queue, but I fail to see how that relates to the _handlers queue.  Why does checking the _handlers queue work here?

Thanks again!

Jon Parise

unread,
Aug 23, 2012, 7:13:54 PM8/23/12
to python-...@googlegroups.com
I took a closer look at this code against Tornado 2.3, and it doesn't
behave quite the same way as when I originally wrote it a couple of
years back. For example, the polling behavior doesn't appear to be
working as it once did. I don't know if something changed in the
surrounding code or in Tornado itself, but I'm afraid I don't have the
time to dig into it at the moment.

If you learn anything useful, however, I'd appreciate hearing about it.

Calvin

unread,
Aug 26, 2012, 6:39:10 PM8/26/12
to python-...@googlegroups.com
I've taken a look through the code, but haven't been able to figure it out myself.

If anyone else has a couple of clock cycles to spare, I'm sure Jon and I would both really appreciate any help.  Again, the question is about how to poll for the completion of all IO handlers and PeriodicCallbacks before shutting down the IOLoop.

Thanks!

A. Jesse Jiryu Davis

unread,
Nov 7, 2012, 5:16:27 PM11/7/12
to python-...@googlegroups.com
Coming to this very late, but I wanted you to know it inspired a use-case for the Lock class in my package, Toro:

https://toro.readthedocs.org/en/latest/examples/lock_example.html

Calvin

unread,
Nov 8, 2012, 1:19:11 PM11/8/12
to python-...@googlegroups.com
This is perfect.  Thank you!
Reply all
Reply to author
Forward
0 new messages