Difference between RequestHandler.write and RequestHandler.flush?

717 views
Skip to first unread message

Sasha

unread,
Nov 9, 2015, 12:03:16 AM11/9/15
to Tornado Web Server
I'm currently learning Tornado, and I tried to understand the difference between RequestHandler.write and RequestHandler.flush, however unfortunately I'm struggling to understand the difference based on the docs - is the only difference that one writes dictionaries to JSON and the other one doesn't? So my question is: what is the difference between the two of them, what can/can't be done, what are best practices, and what security implications must I be aware of?

Cheers,
Sasha

Grégoire

unread,
Nov 9, 2015, 5:31:18 AM11/9/15
to python-...@googlegroups.com
In in the last docs, write and flush doesn't look alike, did you mean write vs finish ?
They are all related though, when you use write, data is not sent right away to the network, it's stored by tornado in a write buffer. This buffer is then sent to the network by the next flush (which is called by finish).
This is important because HTTP responses start with headers, so on the first flush you have to write and send the headers and more importantly, you can't add or modify headers after the first flush.
Tornado allows you to use write (multiple times if you need to), add/modify headers until the first flush which usually occurs automatically at the end.

Most of the time, you just use write and let tornado call flush/finish when it needs to.

finish doesn't include the json conversion shortcut as it is less commonly used I guess.
Concerning security issue mentioned in the write() docs, it seems to have been resolved in recent browsers:


--
You received this message because you are subscribed to the Google Groups "Tornado Web Server" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python-tornad...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ben Darnell

unread,
Nov 9, 2015, 9:23:38 AM11/9/15
to Tornado Mailing List
On Mon, Nov 9, 2015 at 5:31 AM, Grégoire <greg.p...@gmail.com> wrote:
In in the last docs, write and flush doesn't look alike, did you mean write vs finish ?
They are all related though, when you use write, data is not sent right away to the network, it's stored by tornado in a write buffer. This buffer is then sent to the network by the next flush (which is called by finish).
This is important because HTTP responses start with headers, so on the first flush you have to write and send the headers and more importantly, you can't add or modify headers after the first flush.
Tornado allows you to use write (multiple times if you need to), add/modify headers until the first flush which usually occurs automatically at the end.

Most of the time, you just use write and let tornado call flush/finish when it needs to.

Correct. finish() was important when writing asynchronous handlers with callbacks, since it was the only way Tornado knew you had reached the end, but if you're using coroutines you can ignore finish() and just use write().
 

finish doesn't include the json conversion shortcut as it is less commonly used I guess.

finish() and write() do the same json conversion (but it looks like it's only documented for write()). Calling `self.finish(chunk)` is equivalent to `self.write(chunk); self.finish()`, but it's a bit more efficient to do them both in one call.
Reply all
Reply to author
Forward
0 new messages