Search code examples
python-2.7asynchronoustornadopython-multithreading

Python Tornado I/O Loop current() vs instance() method


I have been looking through tornado documents.

While reading on IOLoop, there are contents on which It is saying like below.

In general you should use IOLoop.current as the default when constructing an asynchronous object, and use IOLoop.instance when you mean to communicate to the main thread from a different one.

It makes sense that I have to use instance() method to communicate over multi-threads sharing one global IOLoop instance.

But What is asynchronous object in here and Why should I use current() in the case of asynchronous object?


Solution

  • "Asynchronous object" just means an object like IOStream or HTTPServer which has asynchronous methods.

    You should almost always use IOLoop.current() instead of IOLoop.instance(). In most cases they are equivalent because you only have one thread and one IOLoop, but when they are different current() is usually what you want. The only time when IOLoop.instance() should be used is when A) you have multiple threads and B) a thread other than the IOLoop thread needs to call add_callback (which is the only way that another thread can interact with the IOLoop).