Skip to content

Commit

Permalink
Add some comments about thread safety.
Browse files Browse the repository at this point in the history
  • Loading branch information
bdarnell committed Jan 12, 2011
1 parent 0d7a3f0 commit c270662
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
9 changes: 8 additions & 1 deletion tornado/ioloop.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,14 @@ def remove_timeout(self, timeout):
self._timeouts.remove(timeout)

def add_callback(self, callback):
"""Calls the given callback on the next I/O loop iteration."""
"""Calls the given callback on the next I/O loop iteration.
It is safe to call this method from any thread at any time.
Note that this is the *only* method in IOLoop that makes this
guarantee; all other interaction with the IOLoop must be done
from that IOLoop's thread. add_callback() may be used to transfer
control from other threads to the IOLoop's thread.
"""
self._callbacks.append(stack_context.wrap(callback))
self._wake()

Expand Down
8 changes: 8 additions & 0 deletions tornado/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ def get(self):
See the Tornado walkthrough on http://tornadoweb.org for more details
and a good getting started guide.
Thread-safety notes:
In general, methods on RequestHandler and elsewhere in tornado are not
thread-safe. In particular, methods such as write(), finish(), and
flush() must only be called from the main thread. If you use multiple
threads it is important to use IOLoop.add_callback to transfer control
back to the main thread before finishing the request.
"""

from __future__ import with_statement
Expand Down

0 comments on commit c270662

Please sign in to comment.