define('num_chunks', default=1000)
define('chunk_size', default=2048)
+
class ChunkHandler(RequestHandler):
def get(self):
for i in xrange(options.num_chunks):
self.flush()
self.finish()
+
def main():
parse_command_line()
app = Application([('/', ChunkHandler)])
app.listen(options.port, address='127.0.0.1')
+
def callback(response):
response.rethrow()
assert len(response.body) == (options.num_chunks * options.chunk_size)
logging.warning("fetch completed in %s seconds", response.request_time)
- IOLoop.instance().stop()
+ IOLoop.current().stop()
logging.warning("Starting fetch with curl client")
curl_client = CurlAsyncHTTPClient()
curl_client.fetch('http://localhost:%d/' % options.port,
callback=callback)
- IOLoop.instance().start()
+ IOLoop.current().start()
logging.warning("Starting fetch with simple client")
simple_client = SimpleAsyncHTTPClient()
simple_client.fetch('http://localhost:%d/' % options.port,
callback=callback)
- IOLoop.instance().start()
-
+ IOLoop.current().start()
+
if __name__ == '__main__':
main()
tornado.options.parse_command_line()
http_server = tornado.httpserver.HTTPServer(Application())
http_server.listen(options.port)
- tornado.ioloop.IOLoop.instance().start()
+ tornado.ioloop.IOLoop.current().start()
if __name__ == "__main__":
debug=options.debug,
)
app.listen(options.port)
- tornado.ioloop.IOLoop.instance().start()
+ tornado.ioloop.IOLoop.current().start()
if __name__ == "__main__":
return
http_server = tornado.httpserver.HTTPServer(Application())
http_server.listen(options.port)
- tornado.ioloop.IOLoop.instance().start()
+ tornado.ioloop.IOLoop.current().start()
if __name__ == "__main__":
])
http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(options.port)
- tornado.ioloop.IOLoop.instance().start()
+ tornado.ioloop.IOLoop.current().start()
if __name__ == "__main__":
application = S3Application(root_directory, bucket_depth)
http_server = httpserver.HTTPServer(application)
http_server.listen(port)
- ioloop.IOLoop.instance().start()
+ ioloop.IOLoop.current().start()
class S3Application(web.Application):
app.listen(options.port)
logging.info('Listening on http://localhost:%d' % options.port)
- IOLoop.instance().start()
+ IOLoop.current().start()
if __name__ == '__main__':
main()
tornado.options.parse_command_line()
app = Application()
app.listen(options.port)
- tornado.ioloop.IOLoop.instance().start()
+ tornado.ioloop.IOLoop.current().start()
if __name__ == "__main__":
from tornado.ioloop import IOLoop
IOLoop.configure('tornado.platform.asyncio.AsyncIOLoop')
- IOLoop.instance().start()
+ IOLoop.current().start()
Each ``AsyncIOLoop`` creates a new ``asyncio.EventLoop``; this object
can be accessed with the ``asyncio_loop`` attribute.
if __name__ == "__main__":
application.listen(8888)
- tornado.ioloop.IOLoop.instance().start()
+ tornado.ioloop.IOLoop.current().start()
This example does not use any of Tornado's asynchronous features; for
that see this `simple chat room
tornado.platform.twisted.install()
from twisted.internet import reactor
- When the app is ready to start, call ``IOLoop.instance().start()``
+ When the app is ready to start, call ``IOLoop.current().start()``
instead of ``reactor.run()``.
It is also possible to create a non-global reactor by calling
http_client.close()
"""
def __init__(self, async_client_class=None, **kwargs):
- self._io_loop = IOLoop()
+ self._io_loop = IOLoop(make_current=False)
if async_client_class is None:
async_client_class = AsyncHTTPClient
self._async_client = async_client_class(self._io_loop, **kwargs)
server = HTTPServer(app)
server.listen(8888)
- IOLoop.instance().start()
+ IOLoop.current().start()
In many cases, `tornado.web.Application.listen` can be used to avoid
the need to explicitly create the `HTTPServer`.
server = HTTPServer(app)
server.bind(8888)
server.start(0) # Forks multiple sub-processes
- IOLoop.instance().start()
+ IOLoop.current().start()
When using this interface, an `.IOLoop` must *not* be passed
to the `HTTPServer` constructor. `~.TCPServer.start` will always start
tornado.process.fork_processes(0)
server = HTTPServer(app)
server.add_sockets(sockets)
- IOLoop.instance().start()
+ IOLoop.current().start()
The `~.TCPServer.add_sockets` interface is more complicated,
but it can be used with `tornado.process.fork_processes` to
sock.bind(("", port))
sock.listen(128)
- io_loop = tornado.ioloop.IOLoop.instance()
+ io_loop = tornado.ioloop.IOLoop.current()
callback = functools.partial(connection_ready, sock)
io_loop.add_handler(sock.fileno(), callback, io_loop.READ)
io_loop.start()
.. testoutput::
:hide:
+ By default, a newly-constructed `IOLoop` becomes the thread's current
+ `IOLoop`, unless there already is a current `IOLoop`. This behavior
+ can be controlled with the ``make_current`` argument to the `IOLoop`
+ constructor: if ``make_current=True``, the new `IOLoop` will always
+ try to become current and it raises an error if there is already a
+ current instance. If ``make_current=False``, the new `IOLoop` will
+ not try to become current.
+
+ .. versionchanged:: 4.2
+ Added the ``make_current`` keyword argument to the `IOLoop`
+ constructor.
"""
# Constants from the epoll module
_EPOLLIN = 0x001
Most applications have a single, global `IOLoop` running on the
main thread. Use this method to get this instance from
- another thread. To get the current thread's `IOLoop`, use `current()`.
+ another thread. In most other cases, it is better to use `current()`
+ to get the current thread's `IOLoop`.
"""
if not hasattr(IOLoop, "_instance"):
with IOLoop._instance_lock:
from tornado.platform.select import SelectIOLoop
return SelectIOLoop
- def initialize(self):
- if IOLoop.current(instance=False) is None:
+ def initialize(self, make_current=None):
+ if make_current is None:
+ if IOLoop.current(instance=False) is None:
+ self.make_current()
+ elif make_current:
+ if IOLoop.current(instance=False) is None:
+ raise RuntimeError("current IOLoop already exists")
self.make_current()
def close(self, all_fds=False):
# do stuff...
if __name__ == '__main__':
- IOLoop.instance().run_sync(main)
+ IOLoop.current().run_sync(main)
"""
future_cell = [None]
(Linux), `tornado.platform.kqueue.KQueueIOLoop` (BSD and Mac), or
`tornado.platform.select.SelectIOLoop` (all platforms).
"""
- def initialize(self, impl, time_func=None):
- super(PollIOLoop, self).initialize()
+ def initialize(self, impl, time_func=None, **kwargs):
+ super(PollIOLoop, self).initialize(**kwargs)
self._impl = impl
if hasattr(self._impl, 'fileno'):
set_close_exec(self._impl.fileno())
def on_body(data):
print(data)
stream.close()
- tornado.ioloop.IOLoop.instance().stop()
+ tornado.ioloop.IOLoop.current().stop()
if __name__ == '__main__':
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
stream = tornado.iostream.IOStream(s)
stream.connect(("friendfeed.com", 80), send_request)
- tornado.ioloop.IOLoop.instance().start()
+ tornado.ioloop.IOLoop.current().start()
.. testoutput::
:hide:
tornado.platform.twisted.install()
from twisted.internet import reactor
-When the app is ready to start, call `IOLoop.instance().start()`
+When the app is ready to start, call `IOLoop.current().start()`
instead of `reactor.run()`.
It is also possible to create a non-global reactor by calling
server = TCPServer()
server.listen(8888)
- IOLoop.instance().start()
+ IOLoop.current().start()
2. `bind`/`start`: simple multi-process::
server = TCPServer()
server.bind(8888)
server.start(0) # Forks multiple sub-processes
- IOLoop.instance().start()
+ IOLoop.current().start()
When using this interface, an `.IOLoop` must *not* be passed
to the `TCPServer` constructor. `start` will always start
tornado.process.fork_processes(0)
server = TCPServer()
server.add_sockets(sockets)
- IOLoop.instance().start()
+ IOLoop.current().start()
The `add_sockets` interface is more complicated, but it can be
used with `tornado.process.fork_processes` to give you more
self.assertEqual(id, task_id())
server = HTTPServer(self.get_app())
server.add_sockets([sock])
- IOLoop.instance().start()
+ IOLoop.current().start()
elif id == 2:
self.assertEqual(id, task_id())
sock.close()
(r"/", MainHandler),
])
application.listen(8888)
- tornado.ioloop.IOLoop.instance().start()
+ tornado.ioloop.IOLoop.current().start()
.. testoutput::
:hide:
])
http_server = httpserver.HTTPServer(application)
http_server.listen(8080)
- ioloop.IOLoop.instance().start()
+ ioloop.IOLoop.current().start()
The constructor for this class takes in a list of `URLSpec` objects
or (regexp, request_class) tuples. When we receive requests, we
`.TCPServer.bind`/`.TCPServer.start` methods directly.
Note that after calling this method you still need to call
- ``IOLoop.instance().start()`` to start the server.
+ ``IOLoop.current().start()`` to start the server.
"""
# import is here rather than top level because HTTPServer
# is not importable on appengine
container = tornado.wsgi.WSGIContainer(simple_app)
http_server = tornado.httpserver.HTTPServer(container)
http_server.listen(8888)
- tornado.ioloop.IOLoop.instance().start()
+ tornado.ioloop.IOLoop.current().start()
This class is intended to let other frameworks (Django, web.py, etc)
run on the Tornado HTTP server and I/O loop.